duanrong6802
2013-07-25 10:33
浏览 34
已采纳

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

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.

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • druhoytza979667566 2013-07-25 14:13
    已采纳

    I'm no PHP expert, but I think I see what the issue is . . .

    When you call this code in your JavaScript:

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

    You reload your page with the two selections from the first two <select> dropdowns passed as variables. Then, when the page reloads, you use this code:

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

    . . . to set the selected value when building your first dropdown if the option value matches the city parameter in the URL.

    However, you don't have a similar statement to check the locality parameter from the URL when you are building the second dropdown. So, even though you have passed the parameter (and capture it here: @$locality=$_GET['locality'];), there is no code to use it to figure out which locality option should be selected by default, when the page reloads.

    Like I said, I'm not very good with PHP, but I'd imagine that something like this is needed, when you are building the second dropdown:

    if ($noticia['locality_name']==@$locality) {
        echo "<option selected Value='$noticia[locality_name]'>$noticia[locality_name]</option>"."<BR/>";
    }
    
    已采纳该答案
    打赏 评论
  • dpg76975 2013-07-26 06:55

    The perfectly working code is here:

    PHP code is:

    <?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>
    
    打赏 评论

相关推荐 更多相似问题