Passing Select Multiple to AJAX Script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jcobban
Forum Commoner
Posts: 41
Joined: Mon Mar 08, 2010 7:40 am

Passing Select Multiple to AJAX Script

Post by jcobban »

I have a conventional database update application that I wish to migrate to AJAX. That is I currently have two PHP scripts that both generate web pages, and I wish to change the second to return an XML file so that it can be invoked from a Javascript function called from the first script. So the first PHP script currently looks like:

...
<form ... action='update.php' method='post'>
...
<select name='field[]' multiple='multiple'>
<option>...
</select>
...
<button type='submit'>Submit</button>
...

I want to change the implementation with the minimum effort. In the first script I change the button to:

<button type='button' onclick='sendAjax();'>Update</button>

And the second script I rip out the code to emit the HTML header and trailer and put in:

header("Content-Type: text/xml");
print "<?xml version='1.0' encoding='UTF-8'?>\n" ;
print "<roottag>";
...
print "</roottag>";

However I cannot figure out how to migrate the value of the Select multiple. In the original implementation it is passed by the browser as multiple instances of the key 'field[]' and PHP extracts them into an array called 'field' for me. But in the AJAX implementation the POST arguments are defined in a JS Object as attributes, and I cannot define multiple values of an attribute 'field[]'.

Do I HAVE to change the way the values of the Select are passed, or is there a trick I am missing? All of the examples I see on the web seem to require serializing and unserializing the "array" of values.
Post Reply