View Full Version : Ajax - php - combos

06-16-2009, 04:19 PM
Hi Folks,

my issue is both a php and a javascript (AJAX) one so please forgive it being here - its one or the other. I have two combo boxes one dependant on the other, populated by AJAX but when I submit the form the secondary combo is not being registered as a php $_POST['var'] in the following action page. The secondary combo is contained and altered using divs and inner.HTML. Is this an expected behaviour or is my issue else where?

Please help - im near to tears


06-17-2009, 12:04 AM
Couple of ideas:
1) If you're editing a dropdown (select element) with innerHTML, are you giving it a name attribute when you re-create it?
2) Is the dropdown within the <form> and </form> tags?
3) Try editing the options of the dropdown through the script interface to it instead of messing with / recreating it via innerHTML. Here's an example, assuming an array is returned by the ajax call, and each item of the array has properties 'text' and 'value':

function loadDropdown( responseArray ) {
var dropdown = document.getElementById( 'myDropdown' );
dropdown.options.length = 0; // Start by removing all dropdown options

// Load the new options into the dropdown
for( var i = 0, i < responseArray.length; i++ ) {
dropdown.options[ i ] = new Option( responseArray[ i ].text, responseArray[ i ].value );
If those don't help, post your JavaScript and html code and I can try to help you from there.

06-17-2009, 12:29 PM
Thanks for that - I solved the problem in the end.

What was happening was because my dropdown was changing on the basis of getElementById of the div tag around the "<select ...</select>" My $_POST variable was not being populated.

My workaround was to pass the document.getElementById("var1").value of the select box to some AJAX .....

which sent the value as a querysting to another php file ....

which then set the $_GET variable as a session variable to be used later on

Can I thank myself on this forum?

06-17-2009, 08:45 PM
Oh yikes, that sounds like a seriously hacked workaround for something that shouldn't be an issue. Not to mention the creation of confusing, and down the road, unmaintainable code. Then of course, you have the problem of using a session variable (a shared variable) that could accidentally be overwritten by another page before your original form submission page gets to use it. Talk about a nightmare.

Really, the value from that dropdown should be getting submitted by the form, and therefore be available in the $_POST superglobal when your php page runs. The fact that it's not available is definitely an issue with your javascript code and the method that you are using to repopulate that dropdown.

Again, if you want, post your html/javascript code and I'll take a look as to why that dropdown is not being submitted with the form.

P.S. No you cannot thank yourself in this forum, nor should you for such a seriously hacked approach :p