根据PHP中选择的第一个下拉列表填充第二个下拉列表

时间:2013-05-14 08:01:03

标签: php javascript html

我正在使用PHP和Postgresql / PostGIS数据库。我的数据库模式是这样的。

  1. divname |的 DISTNAME
  2. dhaka |马尼格甘杰
  3. dhaka |纳拉扬甘杰市
  4. dhaka |蒙什甘
  5. 库尔纳|杰索尔
  6. 库尔纳|库什蒂亚
  7. 库尔纳|莫赫尔普
  8. 我使用以下代码

    <?php
    $con = pg_connect("Connection Parameter");
    if (!$con)
      {
      die("Could not connect: " . pg_last_error());
      }
    $divquery = "SELECT DISTINCT divname FROM union_bgd 
            ORDER  BY divname";
    $distquery = "SELECT DISTINCT distname FROM union_bgd
            ORDER  BY distname";
    $resultDiv = pg_query($con, $divquery); 
    $resultDist = pg_query($con, $distquery);
    
        ?>
        Division
      <select id="division" name="division" onclick = "">
      <option value="" selected="selected">Select a Division</option>
      <?php
          while($dd1 = pg_fetch_array( $resultDiv )) 
            {
    
            echo '<option value= "' . $dd1['divname'] . '">' . $dd1['divname'] . '</option>';
    
            }
    
        ?>
    
    </select></br>
    District
    <select id="district" name="district" onclick = "">
      <option value="" selected="selected">Select a District</option>
      <?php
          while($dd2 = pg_fetch_array( $resultDist )) 
            {
    
            echo '<option value= "' . $dd2['distname'] . '">' . $dd2['distname'] . '</option>';
    
            }
    
        ?>
    
    </select>
    
     <?php   
    pg_close($con);
    ?>
    

    目前它填充下拉列表中的所有divname和distname,但我想如果选择“Dhaka”,只有dhaka下的distname将在第二个下拉列表中显示。

    由于

1 个答案:

答案 0 :(得分:-1)

中应该有where条款
$distquery = "SELECT DISTINCT distname FROM union_bgd ORDER  BY distname";

像这样编辑,

$distquery = "SELECT DISTINCT distname FROM union_bgd where divname=".$selected_dist." ORDER  BY distname";