2013-04-23 39 views
0

我正在創建一個簡單的動態刪除列表,第二個的填充是基於第一個的選擇,但問題是,第一個droplist不填充任何東西,所以我可以不使用第二個 任何人都可以幫助我?用AJAX創建動態droplist php mysql

dbconfig.php
<?php 
$host = "localhost"; 
$user = "****"; 
$password = "******"; 
$db = "cat"; 
?> 

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#type").attr("disabled","disabled"); 
      $("select#category").change(function(){ 
      $("select#type").attr("disabled","disabled"); 
      $("select#type").html("<option>wait...</option>"); 
      var id = $("select#category option:selected").attr('value'); 
      $.post("select_type.php", {id:id}, function(data){ 
       $("select#type").removeAttr("disabled"); 
       $("select#type").html(data); 
      }); 
     }); 
     $("form#select_form").submit(function(){ 
      var cat = $("select#category option:selected").attr('value'); 
      var type = $("select#type option:selected").attr('value'); 
      if(cat>0 && type>0) 
      { 
       var result = $("select#type 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 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 /> 
      <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 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; 
    } 

} 
$opt = new SelectList(); 

?> 

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

表結構

CREATE TABLE IF NOT EXISTS `categories` (
    `id_cat` int(4) unsigned NOT NULL AUTO_INCREMENT, 
    `name` varchar(40) NOT NULL, 
    PRIMARY KEY (`id_cat`) 
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ; 

-- 
-- Dumping data for table `categories` 
-- 

INSERT INTO `categories` (`id_cat`, `name`) VALUES 
(1, 'colours'), 
(2, 'flowers'), 
(3, 'tools'); 

-- -------------------------------------------------------- 

-- 
-- Table structure for table `type` 
-- 

CREATE TABLE IF NOT EXISTS `type` (
    `id_type` int(4) unsigned NOT NULL AUTO_INCREMENT, 
    `id_cat` int(4) unsigned NOT NULL, 
    `name` varchar(40) NOT NULL, 
    PRIMARY KEY (`id_type`) 
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=15 ; 

-- 
-- Dumping data for table `type` 
-- 

INSERT INTO `type` (`id_type`, `id_cat`, `name`) VALUES 
(1, 1, 'yellow'), 
(2, 1, 'green'), 
(3, 1, 'red'), 
(4, 1, 'gray'), 
(5, 1, 'white'), 
(6, 2, 'daisy'), 
(7, 2, 'cowslip'), 
(8, 2, 'lily'), 
(9, 2, 'sunflower'), 
(10, 3, 'hammer'), 
(11, 3, 'screwdriver'), 
(12, 3, 'spatula'), 
(13, 3, 'wrench'), 
(14, 3, 'clamp'); 

/*!40101 SET [email protected]_CHARACTER_SET_CLIENT */; 
/*!40101 SET [email protected]_CHARACTER_SET_RESULTS */; 
/*!40101 SET [email protected]_COLLATION_CONNECTION */; 

回答

0

你的主要問題是您試圖訪問ShowCategory使用OPT-$> ShowCategory()

(),但你沒有創建類的SelectList的對象。你應該這樣做

$opt = new SelectList(); 
+0

但我確實創建了$ opt = new SelectList();在select_class.php – user2306354 2013-04-23 07:44:23

+0

可能是對象在其他地方不可用時,當你使用include函數包含它時。 var_dump($ opt)將有助於調試。 – 2013-04-23 07:45:16

+0

我在select _class.php中做了var dump($ opt) – user2306354 2013-04-23 07:54:36