【问题标题】:Change dropdown to dependent dropdown将下拉列表更改为依赖下拉列表
【发布时间】:2020-06-03 12:55:26
【问题描述】:

如下面的代码所示,Semester 下拉菜单和 Programme 下拉菜单当前是分开工作的,它们不相互依赖。

如何将其更改为依赖下拉列表?

例如:当用户选择 31934 学期时,只有该学期(semester)下存在的程序(student_prg)才会显示在程序下拉列表中。

代码

<div class="modal-body">
            <div class="form-group">
                  <label for="title">Select Current Semester:</label>
                  <select name="semester" class="form-control">
                      <option value="">--- Select Current Semester ---</option>

                      <?php
                        require('../setting/config.php');
                          $query = "SELECT DISTINCT semester FROM marketing_data ORDER BY semester DESC"; 
                          $do = mysqli_query($conn, $query);
                          while($row = mysqli_fetch_array($do)){
                              echo '<option value="'.$row['student_matric'].'">'.$row['semester'].'</option>';
                          }
                      ?>
                  </select>
              </div>
              <div class="form-group">
                  <label for="title">Select Programme:</label>
                  <select id="prg" name="prg" class="form-control">
                    <option value="">--- Select Programme ---</option>

                    <?php
                        $query2 = "SELECT student_prg, COUNT(student_prg) as count FROM marketing_data GROUP BY student_prg ORDER BY student_prg DESC"; 
                          $do = mysqli_query($conn, $query2);
                          while($row = mysqli_fetch_array($do)){
                              echo '<option value="'.$row['student_matric'].'" data-count="'.$row['count'].'">'.$row['student_prg'].'</option>';
                          }
                    ?>
                  </select>

截图

数据库

mysql> describe marketing_data;
+---------------+------------------+------+-----+---------+--------------------+
| Field         | Type                 | Null | Key | Default | Extra          |
+---------------+------------------+------+-----+---------+--------------------+
| student_matric| varchar(10) unsigned | NO   | PRI | NULL    | auto_increment |
| student_prg   | text unsigned        | YES  |     | NULL    |                |
| semester      | varchar(10)          | YES  |     | NULL    |                |
| intake_year   | int(10)              | YES  |     | NULL    |                |
| student_city  | text                 | YES  |     | NULL    |                |
| city_lat      | varchar(20)          | YES  |     | NULL    |                |
| city_long     | varchar(20)          | YES  |     | NULL    |                |
| student_state | text                 | YES  |     | NULL    |                |
| state_code    | varchar(100)         | YES  |     | NULL    |                |
+---------------+------------------+------+-----+---------+--------------------+

回复评论@VPC

当我选择学期时,program下拉菜单变为空。

刷新页面后,当我选择program时,所有学期都可以在下拉列表中查看。

【问题讨论】:

  • 数据集有多大?排列组合?
  • 您需要一些 ajax 来根据上述下拉列表的选定值更新第二个下拉列表
  • @SaydFuad ,早些时候我尝试过 phppot.com/demo/… 的 ajax 并根据我的系统更改代码。但是在我的系统中,两个下拉列表来自同一个 MySQL 表。
  • 在我看到的示例中,依赖下拉列表来自不同的 MySQL 表。例如,对于 state 下拉菜单将是 state 表,对于 city 下拉菜单将是 city table。

标签: javascript php html mysql


【解决方案1】:
<div class="modal-body">
    <div class="form-group">
        <label for="title">Select Current Semester:</label>
        <select name="semester" class="form-control">
            <option value="" selected disabled>--- Select Current Semester ---</option> // Don't allow them to re-select this value 

            <?php
            require('../setting/config.php');
            $query = "SELECT DISTINCT semester FROM marketing_data ORDER BY semester DESC";
            $do = mysqli_query($conn, $query);
            while ($row = mysqli_fetch_array($do)) {
                echo '<option value="' . $row['student_matric'] . '">' . $row['semester'] . '</option>';
            }
            ?>
        </select>
    </div>
    <div class="form-group">
        <label for="title">Select Programme:</label>
        <select id="prg" name="prg" class="form-control">
            <option value="" selected disabled>--- Select Programme ---</option> // Don't allow them to re-select this value

            <?php
            //get semester field also the result
            $query2 = "SELECT student_prg , semester, COUNT(student_prg) as count FROM marketing_data GROUP BY student_prg ORDER BY student_prg DESC";
            $do = mysqli_query($conn, $query2);
            while ($row = mysqli_fetch_array($do)) {
                //we are trying to add a common class to all the options 
                //then another class with semester id appended into it so it will be relevent to the specific semester expecting that semester field contains the semester code of other dropdown
                echo '<option class="hidden_options semester_' . $row['semester'] . '" value="' . $row['student_matric'] . '" data-count="' . $row['count'] . '">' . $row['student_prg'] . '</option>';
            }
            ?>
        </select>

        <script>
            //what we are trying to achieve here is when the user selects a semester, first it will hide all the option from programme dropdown then it will only show option which have the class sememter_$semeter_code
            $("select[name='semester']").change(function() {
                var selectValue = $(this).text(); //current semester text i.e semester code
                $('.hidden_options').hide(); //hide all the options
                $('.semester_' + selectValue).show(); //show only options with semester value prefixed into it
            });
        </script>

【讨论】:

  • 感谢您的回复@VPC ... 依赖下拉菜单似乎没有按预期工作。我在上面的屏幕截图中附上了解释。请协助检查。
  • 虽然此代码可以解决问题,including an explanation 说明如何以及为什么解决问题将真正有助于提高您的帖子质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提问的人。请edit您的回答添加解释并说明适用的限制和假设。
  • @DollySha 执行步骤检查发生了什么问题检查选项是否应用了类检查 jquery 是否已获取学期选择选项文本检查 jqeury 代码是否没有任何错误
  • 如果要显示大量数据,更有效的方法当然是使用 ajax。这只是实现目标的技巧。
  • @Dharman 是的,先生。我总是尝试编写可重用的代码。将来,在给某人写答案之前,我一定会记住您的建议。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-10
  • 1970-01-01
  • 2020-04-27
  • 2016-08-13
相关资源
最近更新 更多