2013-04-23 61 views
0

我正在創建2動態下拉列表,第二個是基於第一個的選擇,但問題是第二個不填充,我不知道在哪裏是任何人都可以幫助我的錯誤?php mysql + ajax和jquery dpopulate動態下拉列表

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
  • governorate_name

區:

  • district_id,
  • district_name,
  • governorate_id。

回答

0

在select.php頁面上,您選擇標籤的id是'id ='type'',它將在選擇標籤中用於選擇區域:'id ='區域''。

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' 還是你改變$。員額阿賈克斯查詢。通過'select_type.php'正確發送請求頁面名稱。

+0

我確實改變類型的幫助表示感謝,但這並沒有工作 – user2306354 2013-04-23 09:38:18

+0

哦,你正在'select_district.php'上發送請求,但它應該是'select _type.php'。並且在查詢之前使用'$ _POST ['id']'之前,它是正確的。因爲在ajax調用中,你正在'id'參數中發送值,所以不需要改變'$ _POST ['governorate']'變量。請回答這個問題。或'select_type.php'通過'select_district.php'頁面名稱 – 2013-04-23 12:23:25

+0

@ kishan patel重命名'select_type.php'謝謝你的評論我工作完美,你救了我我問這個問題,所以很多次謝謝youuuu – user2306354 2013-04-23 13:10:18

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'> 
+0

沒有工作,無論我選擇什麼,它都會禁用第二個drop list – user2306354 2013-04-23 09:37:42