从此下拉列表中选择值时,下拉列表会变得清晰

时间:2013-07-25 10:33:42

标签: php javascript

我使用以下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>

当我从第一个下拉列表中选择City时,第二个下拉列表会填充相应的位置名称,当我选择任何人时,它会清除我的选择。第三个下拉列表根据第一个和第二个选择选择并保留在那里。

请让我知道保留第二个下拉值的解决方案。

2 个答案:

答案 0 :(得分:0)

我不是PHP专家,但我想我知道问题是什么。 。

在JavaScript中调用此代码时:

self.location='city-loc.php?city=' + val + '& locality=' +val2;

您使用作为变量传递的前两个<select>下拉列表中的两个选项重新加载页面。然后,当页面重新加载时,您使用以下代码:

if ($noticia2['city_id']==@$city) {
    echo "<option selected Value='$noticia2[city_id]'>$noticia2[city_name]</option>"."<BR/>";
}

。 。 。如果选项值与URL中的city参数匹配,则在构建第一个下拉列表时设置所选值。

但是,在构建第二个下拉列表时,您没有类似的语句来检查URL中的locality参数。因此,即使您已经传递了参数(并在此处捕获它:@$locality=$_GET['locality'];),也没有代码可以使用它来确定在页面重新加载时应默认选择哪个locality选项。

就像我说的那样,我对PHP不是很了解,但是当你构建第二个下拉列表时,我想像这样的东西是需要的:

if ($noticia['locality_name']==@$locality) {
    echo "<option selected Value='$noticia[locality_name]'>$noticia[locality_name]</option>"."<BR/>";
}

答案 1 :(得分:0)

完美的代码在这里:

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)) {
if ($noticia['locality_name']==@$locality) {
    echo "<option selected Value='$noticia[locality_name]'>$noticia[locality_name]</option>"."<BR/>";
}

else{
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>";





echo"</form>";

?>





javascript code is:

<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>