【问题标题】:how to implement chained select using Mysql Php and query如何使用 Mysql Php 和查询实现链式选择
【发布时间】:2014-09-11 07:05:45
【问题描述】:

如何在此代码中添加第三个下拉菜单。我一直无法弄清楚。谢谢!!!

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
    <script type="text/javascript" src="jqueryui/js/jquery-ui-1.8.15.custom.min.js"></script>
        <script type="text/javascript" src="jqueryui/js/jquery-1.6.2.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function(){
                $("select#category").change(function(){
                    var id = $("select#category option:selected").attr('value');
                    $.post("select_type.php", {id:id}, function(data){
                        $("select#type").html(data);
                    });
                });



            });
        </script>
    </head>
    <body>

    <?php 
    include("select.class.php");
    ?>


    <form id="select_form">
        Choose a category:<br />
        <select id="category">
            <?php 
            echo $opt->ShowCategory(); 
            ?>
        </select>
        <br /><br />

        choose a type:<br />
        <select id="type">
            <option value="0">choose...</option>
        </select>
        <br /><br />

        choose third drop down: <br />
        <select id="third_table">
             <option value="0">choose...</option>
        </select>
        <br /><br />  

            <input type="submit" value="confirm" />
        </form>
        <div id="result"></div>
    </body>
</html>

*************************************************************************************************

这是类,我已经添加了查询第三个数据库表的代码

<?php
    class SelectList
    {
        protected $conn;

            public function __construct()
            {
                $this->DbConnect();
            }

            protected function DbConnect()
            {
                include "db_config.php";
                $this->conn = mysql_connect($host,$user,$password) OR die("Unable to connect to the database");
                mysql_select_db($db,$this->conn) OR die("can not select the database $db");
                return TRUE;
            }

            public function ShowCategory()
            {
                $sql = "SELECT * FROM category";
                $res = mysql_query($sql,$this->conn);
                $category = '<option value="0">choose...</option>';
                while($row = mysql_fetch_array($res))
                {
                    $category .= '<option value="' . $row['id_cat'] . '">' . $row['name'] . '</option>';
                }
                return $category;
            }

            public function ShowType()
            {
                $sql = "SELECT * FROM type WHERE id_cat=$_POST[id]";
                $res = mysql_query($sql,$this->conn);
                $type = '<option value="0">choose...</option>';
                while($row = mysql_fetch_array($res))
                {
                    $type .= '<option value="' . $row['id_type'] . '">' . $row['name'] . '</option>';
                }
                return $type;
            }
            public function ShowThird()
            {
                $sql = "SELECT * FROM thirdtable WHERE id_type=$_POST[id2]";
                $res = mysql_query($sql,$this->conn);
                $third = '<option value="0">choose...</option>';
                while($row = mysql_fetch_array($res))
                {
                    $third .= '<option value="' . $row['id_third'] . '">' . $row['name'] . '</option>';
                }
            }
    }

    $opt = new SelectList();
    ?>
    *************************************************************************************************
    select_type.php

    <?php
    include "select.class.php";
    echo $opt->ShowType();
    ?>

【问题讨论】:

    标签: php mysql select cascade chained


    【解决方案1】:

    您的 jquery 更改为:

    <script type="text/javascript">
        $(document).ready(function(){
            $("select#category").change(function(){
                var id = $("select#category option:selected").attr('value');
                $.post("select_type.php?event=type", {id:id}, function(data){
                    $("select#type").html(data);
                });
            });
    
            $("select#type").change(function(){
                var id = $("select#type option:selected").attr('value');
                $.post("select_type.php?event=third_table", {id:id}, function(data){
                    $("select#third_table").html(data);
                });
            });
    
        });
    </script>
    

    您的文件 select_type.php 的结尾更改为:

    ......
    <?php
        include "select.class.php";
        switch ($_GET['event']) {
            case 'third_table':
                    echo $opt->ShowThird();
                break;
    
            default:
                    echo $opt->ShowType();
                break;
        }
    ?>
    

    希望对你有帮助

    【讨论】:

      【解决方案2】:

      HTML 和 PHP

      <h4>Search By Regions</h4>
      <select id="country" name="country">
          <option value="">--</option>
          <?
          $result1 = mysql_query("SELECT * FROM country ORDER BY name");
          while($row1 = mysql_fetch_array($result1)) {
              echo '<option value="'.$row1['name'].'">'.$row1['name'].'</option>';
          }
          ?>
      </select>
      <select id="oblast" name="oblast">
          <option value="">--</option>
          <?
          $result2 = mysql_query("SELECT oblast.name, country.name AS myCountry FROM oblast,country WHERE oblast.country_id = country.id ORDER BY oblast.name");
          while($row2 = mysql_fetch_array($result2)) {
              echo '<option value="'.$row2[0].'" class="'.$row2['myCountry'].'">'.$row2[0].'</option>';
          }
          ?>
      </select>
      <select id="gorod" name="gorod">
          <option value="">--</option>
          <?
          $result3 = mysql_query("SELECT gorod.name, oblast.name AS myOblast FROM gorod,oblast WHERE gorod.region_id = oblast.id ORDER BY gorod.name");
          while($row3 = mysql_fetch_array($result3)) {
              echo '<option value="'.$row3[0].'" class="'.$row3['myOblast'].'">'.$row3[0].'</option>';
          }
          ?>
      </select>
      

      别忘了包括这个

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
      
      <script src="/template/js/jquery.chained.min.js"></script>
      <script>
      $(document).ready(function() {
          $("#oblast").chained("#country");
          $("#gorod").chained("#oblast");
      });
      </script>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-28
        • 2019-04-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多