The cleanest approach by far (in my opinion) is to only have PHP generate a delimited list of the options, and then programmatically add them to the list with JavaScript. You could for example separate items with commas:
Then in JavaScript you would add it programmatically:PHP Code:// Output comma-delimited list of items (assuming $items is an array.)
die(implode(",", $items));
If you want it to be true AJAX (Asynchronous JavaScript and XML), you could generate an XML response (and use the responseXml property):Code:var items = request.responseText.split(","); for (var i = 0; i < items.length; i++) { // ... }
In any case, I don't like the idea of generating HTML in one application and just blindly inserting it into another (using innerHTML.)Code:<?xml version="1.0"?> <items> <item>Item One</item> <item>Item Two</item> </items>




Reply With Quote