PDA

View Full Version : Dropdown From XML



rmagnes
05-26-2009, 08:36 AM
I have a drop down field that is dynamically populated using an XML sheet that looks like:



<content>
<category link="0">
<title>Animals</title>
<xml>xml/animals.xml</xml>
</category>
<category link="1">
<title>Bagua 3 April</title>
<xml>xml/bagua.xml</xml>
</category>
<category link="2">
<title>Anthony Practice</title>
<xml>xml/Anthony.xml</xml>
</category>
</content>


I populate the dropdown box using javascript that looks like this:


<script type="text/javascript">
$(document).ready(function(){
$.ajax({
type: "GET",
url: "galleries.xml",
dataType: "xml",
success: function(xml) {
var select = $('#mySelect');
$(xml).find('category').each(function(){
var title = $(this).find('title').text();
select.append("<option>"+title+"</option>");
});
select.children(":first").text("please make a selection").attr("selected",true);
}
});
});
</script>


My the drop down form looks like this:


<form name="myform" action="dropdown.php" method="post" enctype="multipart/form-data">
<select name="selectBox" id="mySelect" onChange="document.myform.submit()">
<option>loading</option>
</select>
</form>



When a user clicks on the drop down box, all of the values inside "<title>" tags get displayed. Once the user has made their selection, the form is submitted and posts the user's option. My problem is that when the user clicks on the drop down box, I want them to see the content included in the "<title>" text but I want the form to post the data included in the "<xml>" tags.

For example, if the user clicks on the drop down menu and selects the option 'Animals' I want the form to post 'xml/animals.xml'. Any ideas how to handle this?

rmagnes
05-27-2009, 04:46 AM
Hey I finally got this working. For anyone who gets a similar problem, here is what my fix looked like:

[CODE]

$(xml).find('category').each(function(){
var title = $(this).find('title').text();
var val = $(this).find('xml').text();
select.append("<option value='"+val+"'>"+title+"</option>");
});

Reply With Quote Quick reply to this message