简体   繁体   English

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

[英]Populate 2nd dropdown based on 1st dropdown selected in PHP

I am using PHP and Postgresql/PostGIS data base. 我正在使用PHP和Postgresql / PostGIS数据库。 My database schema is something like this. 我的数据库架构是这样的。

  1. divname | divname | distname 地名
  2. dhaka | dhaka | Manikganj 曼尼甘杰
  3. dhaka | dhaka | Narayanganj 纳拉扬甘杰
  4. dhaka | dhaka | Munshiganj 芒希甘杰
  5. Khulna | 库尔纳| Jessore 杰索尔
  6. Khulna | 库尔纳| Kushtia 库什蒂亚
  7. Khulna | 库尔纳| Meherpur 梅赫布尔

I am using the following codes 我正在使用以下代码

<?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);
?>

Currently its populating all the divname and distname in the dropdown but I want if the "Dhaka" is selected only the distname under the dhaka will show in the second dropdown. 当前,它在下拉列表中填充所有divname和distname,但我想如果选择“ Dhaka”,则仅在dhaka下的distname将显示在第二个下拉列表中。

Thanks 谢谢

There should be where clause in 应该有where子句

$distquery = "SELECT DISTINCT distname FROM union_bgd ORDER  BY distname";

Edit like this, 像这样编辑

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据第一个下拉菜单选择填充第二个下拉菜单 - Populate 2nd Dropdown based on 1st Dropdown Choice 基于第一个下拉列表的动态第二个下拉列表 - Dynamic 2nd Dropdown-list based on the 1st Dropdown 根据使用 jquery 和 php 在第一个下拉列表中选择的元素,在第二个下拉列表中添加元素 - add elements in 2nd dropdown list depending upon the element selected in 1st dropdown using jquery and php 如何根据从第一个下拉菜单自动更改的第二个下拉菜单更改第二个文本框的值? - How to change the 2nd textbox value based on the 2nd dropdown that is automatically changed from 1st dropdown? 如何在第1列和第2列的交叉点上填充第3列结果,这些列都来自用户在下拉列表中选择的同一个表? - How can I populate a 3rd column result on intersections of 1st and 2nd columns all from the same table that are selected by the user on dropdown? 通过在codeigniter php中使用ajax,根据第一个下拉结果填充第二个下拉列表 - populate 2nd drop down based on 1st drop down result by using ajax in codeigniter php 从第一个填充第二个列表框 - populate 2nd listbox from 1st 通过选择php中的第一个下拉列表值来填充第二个下拉列表 - populate 2nd dropdown by select first dropdown value in php 如何关联2个下拉列表并在相对于1st的第二个下拉列表中加载数据 - how to relate 2 dropdown and load data in 2nd dropdown in relation to 1st 如何改变 <option>第3个<select>基于第二次变化<select> ,其中第二个是通过使用jquery ajax更改第一个下拉列表的值来绘制的 - How to change <option> of a 3rd <select> based on change in 2nd <select>, where 2nd is drawn by changing value of 1st dropdown using jquery ajax
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM