0

我正在创建 2 个动态下拉列表,第二个是基于第一个的选择,但问题是第二个没有填充,我不知道错误在哪里,谁能帮助我????

数据库配置文件

<?php
$host = "localhost";
$user = "*****";
$password = "****";
$db = "lam_el_chamel_db";
?>

选择.php

<!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="jquery.js"></script>
        <script type="text/javascript">
             $(document).ready(function(){
            $("select#district").attr("disabled","disabled");
            $("select#governorate").change(function(){
            $("select#district").attr("disabled","disabled");
            $("select#district").html("<option>wait...</option>");
            var id = $("select#governorate option:selected").attr('value');
            $.post("select_district.php", {id:id}, function(data){
                $("select#district").removeAttr("disabled");
                $("select#district").html(data);
            });
        });
        $("form#select_form").submit(function(){
            var cat = $("select#governorate option:selected").attr('value');
            var type = $("select#district option:selected").attr('value');
            if(cat>0 && type>0)
            {
                var result = $("select#district option:selected").html();
                $("#result").html('your choice: '+result);
            }
            else
            {
                $("#result").html("you must choose two options!");
            }
            return false;
        });
    });
        </script>
    </head>
    <body>
    <?php include "select.class.php"; ?>
        <form id="select_form">
            Choose a governorate:<br />
            <select id="governorate">

            <?php echo $opt->ShowGovernorate(); ?>


            </select>
            <br /><br />

           choose a district:<br />
            <select id="type">
                <option value="0">choose...</option>
            </select>
            <br /><br />
            <input type="submit" value="confirm" />
        </form>
        <div id="result"></div>
    </body>
</html>

选择类.php

<?php 
 class SelectList
{
    protected $conn;

        public function __construct()

        {
           $this->DbConnect();
        }
    protected function DbConnect()
   {
    include "dbconfig.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 ShowGovernorate()
    {
            $sql = "SELECT * FROM governorate";
            $res = mysql_query($sql,$this->conn);
            $governorate = '<option value="0">choose...</option>';
            while($row = mysql_fetch_array($res))
            {
                $governorate .= '<option value="' . $row['governorate_id'] . '">' . $row['governorate_name'] . '</option>';
            }
            return $governorate;

    }
    public function ShowDistrict()
   {
    $sql = "SELECT * FROM districts WHERE governorate_id=$_POST[id]";
    $res = mysql_query($sql,$this->conn);
    $district = '<option value="0">choose...</option>';
       while($row = mysql_fetch_array($res))
      {
        $district .= '<option value="' . $row['district_id'] . '">' . $row['district_name'] . '</option>';
      }
    return $district;
   }

}   
$opt = new SelectList(); 


?>

选择_type.php

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

表结构

省:

  • 省ID
  • 省名

地区:

  • 区号,
  • 区名,
  • 省ID。
4

2 回答 2

0

在 select.php 页面上,您使用 id 作为选择标签是 'id="type"' 这将是选择标签中的 'id = "district"' 用于选择一个地区:下面的文本。

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

通过以下

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

通过“select_district.php”重命名页面“select_type.php”或更改 $.post ajax 查询。通过“select_type.php”正确发送请求页面名称。

于 2013-04-23T09:26:00.943 回答
0

这应该在

$sql = "SELECT * FROM districts WHERE governorate_id=$_POST[id]";

$sql = "SELECT * FROM districts WHERE governorate_id=$_POST['governorate']";

也在 select.php 改变

<select id='governorate'> to <select id='governorate' name='governorate'>
于 2013-04-23T09:27:54.867 回答