I have 2 drop-down lists using <select>. I want to be able to choose a value from the 1st drop-down list and then automatically populate the 2nd drop-down list based on the value selected in the 1st list. How can I do this using PHP and/or JavaScript?
It can be done either with ajax, or creating arrays in javascript for each 'main' value with the values for the 2nd.. IE: var secondArray = new Array(); secondArray['item1'] = new Array('<?php echo substr(implode("','",$array['item1'],0,-2)); ?>'); secondArray['item2'] = new Array('<?php echo substr(implode("','",$array['item2'],0,-2)); ?>'); secondArray['item3'] = new Array('<?php echo substr(implode("','",$array['item3'],0,-2)); ?>'); Then when the value in the 1st select is changed, use DOM to create the options in the 2nd array for each of the values in the appropriate array. (would be easier, and better to use ajax, theres alot of scripts available if you search google for ajax dropdown)
Do you have a link for the Ajax drop-down lists solution that works? I searched on Google and tried 2 different solutions and both don't do what I need. I need to somehow send a call to the server without refreshing the page and then process which value from the 1st drop-down list was selected, and based on that, populate the 2nd drop-down list without doing a complete page load.
try http://www.webmonkey.com/tutorial/Build_an_Ajax_Dropdown_Menu Edit: actually, that one uses a text file as its database, easy to change but it looks like this one has all the files in a zip http://www.plus2net.com/php_tutorial/ajax_drop_down_list.php
http://www.plus2net.com/php_tutorial..._down_list.php seems to work for me. I did not use a database but checked directly for the selected value and based on that populated the 2nd drop-down list. Thank you killaklown for posting the link.