I've googled this and can only find information on doing this with AS3 - I am using PHP, jQuery and HTML. Is this at all feasable using these technologies and if so... how?
3 Answers
Let say you have the following XML file (songs.xml)
<songs>
<song>
<name>I left my heart on Europa</name>
<id>1</id>
</song>
<song>
<name>Oh Ganymede</name>
<id>2</id>
</song>
<song>
<name>Kallichore</name>
<id>3</id>
</song>
</songs>
With the following PHP code you can generate JSON with the data only from XML file (let's name it xml.php):
<?php
// load the XML file
$songs = simplexml_load_file('songs.xml');
// get all song elements as an array
$options = iterator_to_array($songs->song, false);
// output json
echo json_encode($options);
Now you just need to do a ajax request with jQuery:
$.getJSON("xml.php",function(j){
var $option = $("#option");
$option.empty();
$.each(j, function () {
$option.append($('<option></option>').attr("value", this.id).text(this.name));
});
});
to populate an html code like this:
<select name="x" id="option"></select>
jquery is not tested, but if you already worked with it, you can fix any errors :)
2 Comments
I dont see why not? You want to fill dropbox with elements from XML? By dropdown box you probably mean select option HTML element. There are bunch of XML parsers in PHP like SimpleXML end when you fetch elements just loop in php and set option values.
Pure PHP solution (same XML as Sacx):
<?php
$songs = simplexml_load_file('songs.xml');
echo "<select id='SELsongs'>";
foreach($songs as $song)
{
echo "<option value='".$song->id."'>".$song->name."</option>";
}
echo "</select>";
?>
SimpleXML- web-search that), and convert it into a JSON string (json_encode), then read it using one of the AJAX ops in jQuery. Either create the menu in PHP and return it as an HTML string in your JSON, or return an array in your JSON and assemble the menu in JavaScript. I more often do the former, but circumstances vary.