0

我正在创建三个动态下拉列表,第二个基于第一个的选择,第三个基于第二个的选择,使用 php 和 mysql 检索数据和 javascript

但这并没有发生如何解决问题,因为我在第一个列表中选择的所有内容都显示在第二个和第三个列表中。

在每个表中,我在选择完成之前都有下拉列表的 id 作为外键

谁能帮我 ????

代码

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Register Page</title>
    <link href="style/stylesheet.css" rel="stylesheet" type="text/css" />
    <link href="style/imagesGallery.css"rel="stylesheet" type="text/css"/>


    <SCRIPT language=JavaScript>
    function reload(form)
    {
    var val=form.governorate.options[form.governorate.options.selectedIndex].value; 
    self.location='register.php?governorate=' + val ;
    }

    function reload3(form)
    {
    var val=form.governorate.options[form.governorate.options.selectedIndex].value; 
    var val2=form.district.options[form.district.options.selectedIndex].value; 

    self.location='register.php?governorate=' + val + '&district=' + val2 ;
    }
    </script>
    </head>

    <body>

    <?php

    $governorate = "";
    $district = "";

    //$quer2=mysql_query("SELECT DISTINCT category,cat_id FROM category order by category"); 
    $quer2=mysql_query("SELECT DISTINCT governorate_name,governorate_id FROM governorate order by governorate_name"); 


    //$cat=$_GET['cat']; 
    $governorate=$_GET['governorate'];
    var_dump($governorate);
    if(isset($governorate) and strlen($governorate) > 0)
    {
    $quer=mysql_query("SELECT DISTINCT district_name,district_id FROM districts where governorate_id=$governorate order by district_name"); 
    }
    else
    {
        $quer=mysql_query("SELECT DISTINCT district_name,district_id FROM districts order by district_name"); 
    } 

    $district=$_GET['district'];
    if(isset($district) and strlen($district) > 0){
    $quer3=mysql_query("SELECT DISTINCT village_name FROM village where district_id=$district order by village_name"); 
    }
else
{
$quer3=mysql_query("SELECT DISTINCT village_name FROM village order by village_name"); 
} 

    echo "<select name='governorate' onchange=\"reload(this.form)\"><option value=''>Select one</option>";
    while($noticia2 = mysql_fetch_array($quer2)) 
{ 
    if($noticia2['governorate_id']==@$governorate)
{
echo "<option selected value='$noticia2[governorate_id]'>$noticia2[governorate_name]</option>"."<BR>";
}
    else
 {
  echo  "<option value='$noticia2[governorate_id]'>$noticia2[governorate_name]</option>";
  }
}
    echo "</select>";

    echo "<select name='district' onchange=\"reload3(this.form)\"><option value=''>Select one</option>";
    while($noticia = mysql_fetch_array($quer))
 { 
    if($noticia['district_id']==@$district)
{
echo "<option selected value='$noticia[district_id]'>$noticia[district_name]</option>"."<BR>";
}
    else
  {
  echo  "<option value='$noticia[district_id]'>$noticia[district_name]</option>";
  }
}
    echo "</select>";   

    echo "<select name='village' ><option value=''>Select one</option>";
    while($noticia = mysql_fetch_array($quer3)) 
{ 
    echo  "<option value='$noticia[village_name]'>$noticia[village_name]</option>";
}
    echo "</select>";
    ?>
4

1 回答 1

0

我运行你做的代码,它对我有用。我所做的唯一更改是创建表单元素。我必须创建一个表以使其正常工作。我没有走遍整个村庄。当我让地区工作时,我停止了。

这是我更改的代码:

<?php 
$db = mysql_connect('127.0.0.1','root') or die('could not connect');
mysql_select_db('financial_system_local',$db);

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Register Page</title>
    <link href="style/stylesheet.css" rel="stylesheet" type="text/css" />
    <link href="style/imagesGallery.css"rel="stylesheet" type="text/css"/>


    <SCRIPT language=JavaScript>
    function reload(form)
    {
    var val=form.governorate.options[form.governorate.options.selectedIndex].value; 
    self.location='register.php?governorate=' + val ;
    }

    function reload3(form)
    {
    var val=form.governorate.options[form.governorate.options.selectedIndex].value; 
    var val2=form.district.options[form.district.options.selectedIndex].value; 

    self.location='register.php?governorate=' + val + '&district=' + val2 ;
    }
    </script>
    </head>

    <body>
<form>
    <?php

    $governorate = "";
    $district = "";

    //$quer2=mysql_query("SELECT DISTINCT category,cat_id FROM category order by category"); 
    $quer2=mysql_query("SELECT DISTINCT governorate_name,governorate_id FROM governorate order by governorate_name",$db);
    echo mysql_error($db); 


    //$cat=$_GET['cat']; 
    $governorate=$_GET['governorate'];
    var_dump($governorate);
    if(isset($governorate) and strlen($governorate) > 0)
    {
    $quer=mysql_query("SELECT DISTINCT district_name,district_id FROM districts where governorate_id=$governorate order by district_name",$db); 
    }
    else
    {
        $quer=mysql_query("SELECT DISTINCT district_name,district_id FROM districts order by district_name",$db); 
    } 

    $district=$_GET['district'];
    if(isset($district) and strlen($district) > 0){
    $quer3=mysql_query("SELECT DISTINCT village_name FROM village where district_id=$district order by village_name"); 
    }
else
{
$quer3=mysql_query("SELECT DISTINCT village_name FROM village order by village_name"); 
} 

    echo "<select name='governorate' onchange=\"reload(this.form)\"><option value=''>Select one</option>";
    while($noticia2 = mysql_fetch_array($quer2)) 
{ 
    if($noticia2['governorate_id']==@$governorate)
{
echo "<option selected value='$noticia2[governorate_id]'>$noticia2[governorate_name]</option>"."<BR>";
}
    else
 {
  echo  "<option value='$noticia2[governorate_id]'>$noticia2[governorate_name]</option>";
  }
}
    echo "</select>";

    echo "<select name='district' onchange=\"reload3(this.form)\"><option value=''>Select one</option>";
    while($noticia = mysql_fetch_array($quer))
 { 
    if($noticia['district_id']==@$district)
{
echo "<option selected value='$noticia[district_id]'>$noticia[district_name]</option>"."<BR>";
}
    else
  {
  echo  "<option value='$noticia[district_id]'>$noticia[district_name]</option>";
  }
}
    echo "</select>";   

    echo "<select name='village' ><option value=''>Select one</option>";
    while($noticia = mysql_fetch_array($quer3)) 
{ 
    echo  "<option value='$noticia[village_name]'>$noticia[village_name]</option>";
}
    echo "</select>";
    ?>
    </form>
于 2013-04-23T01:49:57.797 回答