所以事情是,我有4个下拉菜单,如果我选择第一个下拉菜单是父菜单,那么它将基于第一个选择填充产品名称。如果选择了第二个和第三个下拉值,则它将过滤第四个上的数据
我面临的问题是我无法为此找到解决方案,因为我必须全选才能获得产品,并且如果我没有像电信这样的子类别,那么我将无法获得产品。
我已经尝试过ajax方法:
<script type="text/javascript">
$(document).ready(function(){
$('#parentt').on('change',function(){
var parentID = $(this).val();
if(parentID){
$.ajax({
type:'POST',
url:'ajaxData.php',
data:'parent_id='+parentID,
success:function(html){
$('#sub').html(html);
$('#subsub').html('<option value="">Select sub first</option>');
}
});
}else{
$('#sub').html('<option value="">Select parent first</option>');
$('#subsub').html('<option value="">Select sub first</option>');
}
});
$('#sub').on('change',function(){
var subID = $(this).val();
if(subID){
$.ajax({
type:'POST',
url:'ajaxData.php',
data:'sub_id='+subID,
success:function(html){
$('#subsub').html(html);
}
});
}else{
$('#subsub').html('<option value="">Select sub first</option>');
}
});
$('#subsub').on('change',function(){
var subsubID = $(this).val();
if(subsubID){
$.ajax({
type:'POST',
url:'ajaxData.php',
data:'subsub_id='+subsubID,
success:function(html){
$('#pro').html(html);
}
});
}else{
$('#pro').html('<option value="">Select sub sub category first</option>');
}
});
});
</script>
<body >
<div class="select-boxes">
<?php
include('dbConfig.php');
$query = $db->query("SELECT * FROM categories where parent IS NULL");
$rowCount = $query->num_rows;
?>
<select name="parentt" id="parentt" >
<option value="">Select Main Category</option>
<?php
if($rowCount > 0){
while($row = $query->fetch_assoc()){
echo '<option value="'.$row['category_id'].'">'.$row['category_name'].'</option>';
}
}else{
echo '<option value="">Category not available</option>';
}
?>
</select>
<select name="sub" id="sub">
<option value="">Select parent first</option>
</select>
<select name="subsub" id="subsub">
<option value="">Select sub category first</option>
</select>
<select name="pro" id="pro">
<option value="">Select sub sub category first</option>
</select>
</div>
</body>
----------------------------ajaxData.php-----------------------
if(isset($_POST["parent_id"]) && !empty($_POST["parent_id"])){
$query = $db->query("SELECT * FROM categories WHERE parent = ".$_POST['parent_id']."");
$rowCount = $query->num_rows;
if($rowCount > 0){
echo '<option value="">Select sub category</option>';
while($row = $query->fetch_assoc()){
echo '<option value="'.$row['category_id'].'">'.$row['category_name'].'</option>';
}
}else{
echo '<option value="">Sub category not available</option>';
}
}
if(isset($_POST["sub_id"]) && !empty($_POST["sub_id"])){
$query = $db->query("SELECT * FROM categories WHERE parent = ".$_POST['sub_id']."");
$rowCount = $query->num_rows;
if($rowCount > 0){
echo '<option value="">Select sub sub category</option>';
while($row = $query->fetch_assoc()){
echo '<option value="'.$row['category_id'].'">'.$row['category_name'].'</option>';
}
}else{
echo '<option value="">sub sub category not available</option>';
}
}
if(isset($_POST["subsub_id"]) && !empty($_POST["subsub_id"])){
$query = $db->query("SELECT * FROM products WHERE category_id = ".$_POST['subsub_id']."");
$rowCount = $query->num_rows;
if($rowCount > 0){
echo '<option value="">Select product</option>';
while($row = $query->fetch_assoc()){
echo '<option value="'.$row['product_id'].'">'.$row['product_name'].'</option>';
}
}else{
echo '<option value="">product not available</option>';
}
}
即使未选择子或子子,我也希望在选择父子时加载产品,选择子或子子后,数据将被相应过滤