我想从我的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']! –