0

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

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

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

4

2 回答 2

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/>";
}

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

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

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

if ($noticia['locality_name']==@$locality) {
    echo "<option selected Value='$noticia[locality_name]'>$noticia[locality_name]</option>"."<BR/>";
}
于 2013-07-25T14:13:24.737 回答
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>
于 2013-07-26T06:55:21.830 回答