我使用下面的 HTML 和 PHP 代码创建了三个级联下拉列表
<?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>";
?>
我正在为城市和地区下拉列表的 onchange 事件使用下面的 javascript 函数
<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>
当我从第一个下拉列表中选择城市时,第二个下拉列表填充相应的地区名称,当我选择任何人时,它会清除我的选择。根据第一个和第二个选择选择的第三个下拉列表并保留在那里。
请让我知道保留第二个下拉值的解决方案。