简体   繁体   English

如何在另一个组合框所选值的基础上填充一个组合框

[英]How to fill one combo box basing other combo box selected value

I have created two tables "category" and "subcategory". 我创建了两个表“ category”和“ subcategory”。 So what i need to do is that i have organized two combobox and one will retrieve values from category table and populate the data. 因此,我需要做的是组织了两个组合框,一个组合框将从类别表中检索值并填充数据。 However, i need to fill the other combobox when one selected from the category combobox and populate all the values from subcategory table. 但是,当从类别组合框中选择一个时,我需要填充另一个组合框,并填充子类别表中的所有值。

I really thank you all for helping me out. 我真的非常感谢大家的帮助。

Looking for great answer. 寻找很好的答案。

Thank you in advance. 先感谢您。

You will probably want to use AJAX and php to do this.... 您可能要使用AJAX和php来做到这一点。

First, if you don't have a jquery library, you can get it at http://jquery.com/download/ This will enable you to use AJAX jquery. 首先,如果没有jquery库,则可以从http://jquery.com/download/获得它。这将使您能够使用AJAX jquery。

In your html header: 在您的html标头中:

    <script>

       $(document).ready(function() {
        $("#category").change(function() {

            var selectVal = $('#category :selected').text();


            $.ajax({                                      
              url: 'getsubcat.php',                         
              data: "groupid="+selectVal,                                                     
              type:'post',
              //dataType: 'json',                
              success: function(data)    
              { 
                $('#subcat').html('<option value="">'+data+'</option>');
              }
            });


        });

      });

    </script>

In the html body -- 在html正文中

<?php
    include('conn.php');    
        $sql = "SELECT * FROM categories";
        $result3 = pg_query($con, $sql);
      ?>


<!-- first select list -->

Select a category: </td><td>
        <select id="category" name="category">
          <option value = ""></option>
        <?php
          while($row = pg_fetch_array($result3)) {
            echo '<option >'.$row[1].'</option>';
          }
        ?> 
        </select>



<?php

        $sql = "SELECT * FROM subcategories where cat = '$cat'";
        $result3 = pg_query($con, $sql);

      ?>

      Select a sub category: 
      <select id="subcat" name="subcat">
        <option value = ""></option>
        <?php
          while($row = pg_fetch_array($result3)) {
            echo '<option>'.$row[1].'</option>';
        }
      ?>


      </select>

Finally, in your php file -- getsubcat.php 最后,在您的php文件中-getsubcat.php

<?php
    $category = $_POST['groupid'];



  include('conn.php');
  $sql="select * from subcat where cat_name = '$category'";
  $result = pg_query($con, $sql);

  while($row = pg_fetch_array($result)) { 

    echo '<option>'.$row[1].'</option>';


  }
  pg_query($sql) or die("Error: ".pg_last_error());
?>

All of this will allow you to have your select lists dynamic an dependent on the first one. 所有这些使您可以使选择列表动态依赖于第一个列表。

Here I am using postgresql for the database, but you should be able to change the connection string (conn.php) and the table names, columns if you are using a different database like mysql. 在这里,我使用的是Postgresql数据库,但是如果使用的是像mysql这样的其他数据库,则应该能够更改连接字符串(conn.php)和表名,列。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM