php mysql + 3动态下拉列表框

时间:2013-04-22 19:06:29

标签: php javascript mysql drop-down-menu

我正在创建三个动态下拉列表,第二个是基于第一个和第三个的选择是基于第二个选择使用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>";
    ?>

1 个答案:

答案 0 :(得分: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>