I have created three cascading dropdownlist using below HTML and PHP code
<?php
@$city=$_GET['city'];
@$locality=$_GET['locality'];// Use this line or below line if register_global is off
if(strlen($city) > 0 and !is_numeric($city)){ // to check if $city is numeric data or not.
echo "Data Error";
exit;
}
if(strlen($locality)>0 and is_numeric($locality)){
echo "Data Issue";
exit;
}
///////// Getting the data from Mysql table for first list box//////////
$quer2=mysql_query("SELECT DISTINCT city_name,city_id FROM city order by city_name");
///////////// End of query for first list box////////////
/////// for second drop down list we will check if category is selected else we will display all the subcategory/////
if(isset($city) and strlen($city) > 0){
$quer=mysql_query("SELECT DISTINCT locality_name FROM locality where city_id='$city' order by locality_name");
}else{$quer=mysql_query("SELECT DISTINCT locality_name FROM locality order by locality_name"); }
////////// end of query for second subcategory drop down list box ///////////////////////////
echo "<form method=post name=f1>";
/// Add your form processing page address to action in above line. Example action=dd-check.php////
////////// Starting of first drop downlist /////////
echo "<select name='city' onchange=\"localityload(this.form)\"><option value=''>Select one</option>";
while($noticia2 = mysql_fetch_array($quer2)) {
if($noticia2['city_id']==@$city){echo "<option selected value='$noticia2[city_id]'>$noticia2[city_name]</option>"."<BR/>";}
else{echo "<option value='$noticia2[city_id]'>$noticia2[city_name]</option>";}
}
echo "</select>";
////////////////// This will end the first drop down list ///////////
////////// Starting of second drop downlist /////////
echo "<select name='locality' id='locality' onchange=\"localityload(this.form);\"><option selected value=''>Select one</option>";
while($noticia = mysql_fetch_array($quer)) {
echo "<option value='$noticia[locality_name]' >$noticia[locality_name]</option>";
}
echo "</select>";
if(isset($locality) and strlen($locality)>0)
{
$query=mysql_query("SELECT DISTINCT specialization FROM drsignup WHERE locality_name='$locality'");
}
else {$query=mysql_query("SELECT DISTINCT specialization FROM drsignup");}
echo "<select name='specialization'>";
echo "<option selected='selected' value='Specialist In'>Specialist In</option>";
while($notic = mysql_fetch_array($query))
{
echo "<option value='$notic[specialization]'>$notic[specialization]</option>";
}
echo "</select>";
<!--<input type="submit" name="submit" value="SignUp" />-->
echo "</form>";
?>
I am using below javascript function for the onchange event for city and locality dropdown list
<script language=JavaScript>
function localityload(form)
{
var val=form.city.options[form.city.options.selectedIndex].value;
var val2=form.locality.options[form.locality.options.selectedIndex].value;
self.location='city-loc.php?city=' + val + '& locality=' +val2;
form.locality.options[form.locality.options.selectedIndex].selected=true;
}
</script>
When I select City from first dropdown,second drop down populate corresponding locality name and when i select anyone it gets clear my selection .The third dropdown list selected based on first and second selection and it retains there.
Please let me know the solution to retain the second dropdown value.