I'm working on a chained select drop down that pulls data from the database to populate the category combobox while the subcategory combo box populates when an option from the category combobox is selected. 类别下拉框从数据库中提取数据没有任何问题,但是我无法根据类别组合框中选择的选项的“id”从数据库中获取子类别组合框选择数据。任何帮助请?下面是我的 php 代码以及显示数据的 jquery 代码
<?php
include ('../storescripts/connection.php');
function ShowCategory(){
$sql = "SELECT * FROM category";
$res = mysql_query($sql) or die(mysql_error());
$category = '<option value="0">Choose...</option>';
while($row = mysql_fetch_array($res))
{
$category .= '<option value = "'.$row['category_id'].'"> '.$row['category_name']. ' </option>';
}
return $category;
}
function ShowSubCategory(){
if (!isset($_POST['id']))
{
//If not isset -> set with dumy value
$_POST['id'] = "";
}
$sql1 = "SELECT * FROM subcategory WHERE category_id = '$_POST[id]'";
$res1 = mysql_query($sql1) or die(mysql_error());
$subcategory = '<option value="0"> Choose...</option>';
while($row = mysql_fetch_array($res1)){
$subcategory .= '<option value="'.$row['subcategory_id'].'"> '.$row['subcategory_name'].' </option>';
}
return $subcategory;
}
?>
//jquery代码
<script type="text/javascript">
$(document).ready(function() {
$("select#category").change(function(){
var id = $("select#category option:selected").attr('value');
$.post("select_subcat.php", {id:id}, function(data){
$("select#subcategory").html(data);
});
});
});
</script>