0

我正在使用 php mysql + ajax jquery 创建动态下拉列表,第二个下拉列表的填充基于第一个和第三个的选择是基于第二个的选择,但它没有用任何人都可以帮助我???

数据库配置文件

<?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#village").attr("disabled","disabled");
            $("select#governorate").change(function(){

            $("select#district").attr("disabled","disabled");
            $("select#district").html("<option>wait...</option>");

            $("select#village").attr("disabled","disabled");
            $("select#village").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);
            });

            var id2 = $("select#district option:selected").attr('value');
            $.post("select_village.php", {id:id}, function(data){
                $("select#village").removeAttr("disabled");
                $("select#village").html(data);
            });

        $("form#select_form").submit(function(){
            var gover = $("select#governorate option:selected").attr('value');
            var dist = $("select#district option:selected").attr('value');



            if(gover>0 && dist>0)
            {
                var result = $("select#district option:selected").html();
                $("#result").html('your choice: '+result);
            }
            else
            {
                $("#result").html("you must choose two options!");
            }
            if(dist>0 && village>0)
            {
                var result = $("select#village option:selected").html();
                $("#result").html('your choice: '+result);
            }
             else
            {
                $("#result").html("you must choose three 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="district">
                <option value="0">choose...</option>
            </select>
            <br /><br />

            choose a village:<br />
            <select id="village">
                <option value="0">choose...</option>
            </select>
            <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);
    var_dump($res);
    $district = '<option value="0">choose...</option>';
       while($row = mysql_fetch_array($res))
      {
        $district .= '<option value="' . $row['district_id'] . '">' . $row['district_name'] . '</option>';
      }
    return $district;
   }

   public function ShowVillage()
   {
    $sql = "SELECT village_id, village_name FROM village WHERE district_id=$_POST[id2]";
    $res = mysql_query($sql,$this->conn);
    $village = '<option value="0">choose...</option>';
       while($row = mysql_fetch_array($res))
       {
         $village .='<option value="' .$row['village_id'] . '">' . $row['village_name'] . '</option>';
       }
       return $village;
   }


}   
$opt = new SelectList(); 


?>

选择区.php

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

select_village.php

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

我认为它在 select.php 中有一些东西,但我不知道错误是什么

4

1 回答 1

0

您的 ajax 调用是在 document.ready 函数上完成的。您应该将 ajax 绑定到下拉更改功能,例如:

$("select#district option:selected").change(function(){
id = $(this).val(); 
$.post("select_village.php", {id:id}, function(data){
    $("select#village").removeAttr("disabled");
    $("select#village").html(data);
});

希望有帮助

于 2013-04-24T14:28:02.257 回答