1) when types selected from dropdown his category and subcategory should be shown
in dropdown,problem : when we select types in category it is showing all the category
and all the subcategory of other types also
please solve this problem as soon as possible.
inventory_list.php
<select name="typ_id" id="typ_id">
<option>Select a Type</option>
<?php
$get_types = "select * from types";
$run_types = mysql_query($get_types);
while ($row_types=mysql_fetch_array($run_types)){
$typ_id= $row_types['typ_id'];
$typ_name = $row_types['typ_name'];
echo "<option value='$typ_id'>$typ_name</option>";
}
?>
<select name="cat_id" id="cat_id">
<option>Select a Category</option>
<?php
$get_category = "SELECT * FROM category ";
$run_category = mysql_query($get_category);
while ($row_category=mysql_fetch_array($run_category)){
$cat_id= $row_category['cat_id'];
$cat_name = $row_category['cat_name'];
echo "<option value='$cat_id'>$cat_name</option>";
}
?>
</select>
答案 0 :(得分:0)
尝试以下代码
<script>
function get_category(type_id){
$.ajax({
type: "POST",
url: "get_category.php",
data: { type_id : type_id }
}).done(function(data){
$("#category").html(data);
});
}
</script>
<select name="typ_id" id="typ_id" onchange="get_category(this)">
<option>Select a Type</option>
<?php
$get_types = "select * from types";
$run_types = mysql_query($get_types);
while ($row_types=mysql_fetch_array($run_types)){
$typ_id= $row_types['typ_id'];
$typ_name = $row_types['typ_name'];
echo "<option value='$typ_id'>$typ_name</option>";
}
?>
<div id='category'></div>
get_category.php
<?php
$get_category = "SELECT * FROM category where type_id = $_POST['type_id']";
$run_category = mysql_query($get_category);
echo '<select name="cat_id" id="cat_id">
<option>Select a Category</option>';
while ($row_category=mysql_fetch_array($run_category)){
$cat_id= $row_category['cat_id'];
$cat_name = $row_category['cat_name'];
echo "<option value='$cat_id'>$cat_name</option>";
}
echo '</select>';
?>