我想從我的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>
這個解釋有點雜亂,但我希望任何人都可以幫助我,並給我一些很好的建議。
謝謝。
嘗試使用Chrome並使用開發工具來檢查發生了什麼。打開開發窗口,然後點擊網絡選項卡,清除所有內容,然後點擊提交。回到你的開發標籤並查看響應。另請看控制檯選項卡。這應該給你一些提示。 – MrTechie
在你正在傳遞chainmenu.php,但在function.php中使用$ _POST ['id']! –