2015-04-07 83 views
0

我想從我的html中選擇的值添加到使用jQuery的PHP文件中。我有2個PHP文件,chainmenu.php和function.php。 function.php包含2個函數來從我的數據庫中獲取一些數據。 chainmenu.php用於顯示函數function.php的結果。它需要一個變量,這是我html中選定選項的值。我能夠檢索值,但我的問題是,我的$ .post函數不起作用。我不知道錯誤在哪裏,是在我的chainmenu.php還是在我的function.php中。

這是我的代碼

jQuery代碼

<script type="text/javascript"> 
     $(document).ready(function() { 
      $("select#trafo").attr("disabled","disabled"); 
      $("select#gi").change(function(){ 
       $("select#trafo").attr("disabled","disabled"); 
       $("select#trafo").html("<option>wait...</option>"); 
       var id = $("select#gi option:selected").attr('value'); 
       $("select#trafo").html("<Option>"+id+"</Option>"); 
       $.post("chainmenu.php", {id:id}, function(data){ 
        $("select#trafo").removeAttr("disabled"); 
        $("select#trafo").html(data); 
       }); 
      }); 
     }); 
     </script> 

Function.php

<?php class SelectList 
    { 

//$this->conn is working fine here 

    public function ShowGI() 
      { 
       $sql = "SELECT * FROM gi"; 
       $res = mysqli_query($this->conn,$sql); 
       if(mysqli_num_rows($res)>=1){ 
        $category = '<option value="0">Pilih GI</option>'; 
        while($row = mysqli_fetch_array($res)) 
        { 
         $category .= '<option value="' . $row['idgi'] . '">' . $row['namegi'] . '</option>'; 
        } 
       } 
       return $category; 
      } 

      public function ShowIt() 
      { 
       $sql = "SELECT * FROM It WHERE idgi=$_POST[id]"; 
       $res = mysql_query($sql,$this->conn); 
       $type = '<option value="0">Choose/option>'; 
       while($row = mysql_fetch_array($res)) 
       { 
        $type .= '<option value="' . $row['idIt'] . '">' . $row['name'] . '</option>'; 
       } 
       return $type; 
      } 
       } 

    $opt = new SelectList(); 
    ?> 

chainmenu.php

<?php include "/opsi.class.php"; 
echo $opt->ShowIt(); ?> 

HTML代碼

<head> 
<!-- the script here --> 
</head> 
<body> 
<select id=gi> 
<option value="0"> Select </option> 
</select> 
<select id=It> 
<!-- chainmenu.php result should be here --> 
</select> 

</body> 

這個解釋有點雜亂,但我希望任何人都可以幫助我,並給我一些很好的建議。

謝謝。

+0

嘗試使用Chrome並使用開發工具來檢查發生了什麼。打開開發窗口,然後點擊網絡選項卡,清除所有內容,然後點擊提交。回到你的開發標籤並查看響應。另請看控制檯選項卡。這應該給你一些提示。 – MrTechie

+0

在你正在傳遞chainmenu.php,但在function.php中使用$ _POST ['id']! –

回答

0

嘗試像這樣在chainmenu.php

<?php include "/opsi.class.php"; 
echo $opt->ShowIt($_POST['id']); ?> 

在function.php取代ShowIt()方法如下圖所示,

public function ShowIt($id) 
      { 
       $sql = "SELECT * FROM It WHERE idgi=$id"; 
       $res = mysql_query($sql,$this->conn); 
       $type = '<option value="0">Choose/option>'; 
       while($row = mysql_fetch_array($res)) 
       { 
        $type .= '<option value="' . $row['idIt'] . '">' . $row['name'] . '</option>'; 
       } 
       return $type; 
      } 
+0

感謝您的建議。但它仍然無法正常工作。 – anjaryes

+0

在jquery中alert(id)是什麼來的? –

+0

chainmenu.php中的echo var_dump($ _ POST)給出了什麼? –

0

有幾個錯別字ShowIt()用於如$type = '<option value="0">Choose/option>';功能標籤沒有正確關閉。 在jQuery代碼中,您正在檢索值並添加html以選擇具有id trafo的標記。而在html代碼中,select的id是It。

<select id=It> 
<!-- chainmenu.php result should be here --> 
</select> 
+0

是的,我承認它。我會編輯它。 當我修復這些語法錯誤後,我發現主要問題是$ .post命令。我的chainmenu.php無法從腳本中獲取「id」變量.. – anjaryes