0

2 番目のリストは最初のリストの選択に基づいているという 2 つの動的ドロップ リストを作成していますが、問題は 2 番目のリストが入力されず、エラーの場所がわからないことです。

dbconfig.php

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

select.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>

class.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();
?>

テーブル構造

県 :

  • Governorate_id
  • 県名

地区:

  • 地区ID、
  • 地区名、
  • Governorate_id。
4

2 に答える 2

0

select.php ページで select タグに id を使用したのは 'id="type"' で、これは select タグの '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_type.php」から「select_district.php」に変更するか、$.post ajax クエリで変更します。「select_type.php」で送信リクエストページ名を修正。

于 2013-04-23T09:26:00.943 に答える