PDA

View Full Version : Chained dropdowns dynamically created with PHP!!Javscript help needed now..



kat_T
08-06-2010, 10:47 AM
Hi,

I have created two dynamic drop down menus one for collectors and one for location from ms sql server database using PHP. The code is the following:


$sql="SELECT DISTINCT collector from my.table where long IS NOT NULL order by collector";

$result = sqlsrv_query( $conn, $sql);


echo "<select id='collector' name='collector'>\n"; //create the drop down menu
echo "\t<option value=\"SELECT COLLECTOR\" ";
if ($_POST['collector']=="SELECT COLLECTOR") echo "selected=\"selected\"";
echo ">---SELECT COLLECTOR---</option>\n";
while($nt=sqlsrv_fetch_array($result)){//Array or records stored in $nt
echo "\t<option value=\"{$nt['collector']}\"";
if ($_POST['collector']==$nt['collector']) echo "selected=\"selected\"";
echo ">$nt[collector]</option>\n";
/* Option values are added by looping through the array */
}
echo "</select>";// Closing of list box


and the other drop down is created exactly the same way.

So the question now is:
How do I make them dependant (chained) so that when I select an option in the first drop down then the values in the second will change??

I have seen plenty examples on how to implement this (with javascript and AJAX) but none for something like the above where the drop downs are both dynamically populated.

NB!! Both the collectors and the location are in the same table. So if I select ie. Adams, N. I want the second drop down to show the locations where Adams has been.

Thank you in advance
Kat