Reputation: 445
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?
Upvotes: 0
Views: 5812
Reputation: 806
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>";
?>
Upvotes: 0
Reputation: 6392
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 :)
Upvotes: 3
Reputation: 513
You have to import the xml and parse it, this will give you an object or multidimensional array which you can loop through and thus creating the elements for the dropdown.
Upvotes: 0