如何显示属于某个类别的子类别?

如何显示属于某个类别的子类别?

问题描述:

此代码是两个类别和subcategory.In类我显示在下拉列表中的所有值,但子类别我想说明这是属于一个类别的准确子类别。请帮我如何显示属于某个类别的子类别?

<div class="form-group"> 
    <label for="exampleInputEmail1">Category Name</label> 
    <select name="type" id="type"> 
     <option>Select Category</option> 
     <?php 
      $res= mysql_query("select * from ".CATEGORY." order by id asc"); 
      while($data=mysql_fetch_array($res)) 
      { 
     ?> 
     <option value="<?php echo $data['id'];?>"><?php echo $data['cat_name'];?></option> 
     <?php } ?> 
    </select> 
</div> 
<div class="form-group"> 
    <label for="exampleInputEmail1">Subcategory Name</label> 
    <select name="type1" id="type1"> 
     <option>Select Category</option> 
     <?php $res= mysql_query("select * from ".SUBCATEGORY." order by id asc"); 
     while($data=mysql_fetch_array($res)) 
     { 
     ?> 
     <option value="<?php echo $data['id'];?>"><?php echo $data['sub_name'];?></option> 
     <?php } ?> 
    </select> 
</div>        
+0

使用AJAX来获取子类 – Khushboo 2014-10-08 09:28:52

在它使用jQuery时,一类是选择将需要价值和从阿贾克斯价值就会发布到ajax.php文件

<div class="form-group"> 
    <label for="exampleInputEmail1">Category Name</label> 
    <select name="type" id="type"> 
     <option>Select Category</option> 
     <?php 
      $res= mysql_query("select * from ".CATEGORY." order by id asc"); 
      while($data=mysql_fetch_array($res)) 
      { 
     ?> 
     <option value="<?php echo $data['id'];?>"><?php echo $data['cat_name'];?></option> 
     <?php } ?> 
    </select> 
</div> 
<div class="form-group"> 
    <label for="exampleInputEmail1">Subcategory Name</label> 
    <select name="type1" id="type1"> 


    </select> 
</div> 


<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript"></script> 
<script type="text/javascript"> 
$(document).ready(function(){ 

    $('#type').on("change",function() { 
     var categoryId = $(this).find('option:selected').val(); 
     $.ajax({ 
      url: "ajax.php", 
      type: "POST", 
      data: "categoryId="+categoryId, 
      success: function (response) { 
       console.log(response); 
       $("#type1").html(response); 
      }, 
     }); 
    }); 

}); 

</script> 

在同一目录

做一个PHP文件命名为ajax.php,并把这个代码

<?php 
$categoryId = $_POST['categoryId']; 
echo "<option>Select Category</option>"; 
$res= mysql_query("select * from ".SUBCATEGORY." WHERE category_id = $categoryId order by id asc"); 
     while($data=mysql_fetch_array($res)) 
     { 
     echo "<option value='".$data['id']."'>."$data['sub_name']."</option>"; 
     } 
?> 

这将工作

+0

非常感谢你。它真的工作并帮助了我很多 – Shaddy 2014-10-08 10:55:22

+0

我的乐趣:) .. – 2014-10-08 11:42:02

您查询是错误的,从表名称中删除quete

$res= mysql_query("select * from ".CATEGORY." order by id asc"); 
$res= mysql_query("select * from ".SUBCATEGORY." order by id asc"); 

这样

$res= mysql_query("select * from CATEGORY order by id asc"); 
$res= mysql_query("select * from SUBCATEGORY order by id asc"); 

在我看来,你可能公顷在第二个查询中添加where子句。

您需要执行AJAX请求onCategoryClick或生成Java代码与子类别的数组属于类别,并在它们之间切换js。 https://*.com/a/11238038/3711660 - 关于AJAX请求

尝试这可能会帮助你......

<?php 
if($_GET['id']) 
{ 
    $id=$_GET['id']; 
} 
?>       
<script> 
function newDoc(str) 
{ 
    window.location.assign("your_page_name.php?id="+str) 
} 
</script> 

<div class="form-group"> 
    <label for="exampleInputEmail1">Category Name</label> 
    <select name="type" id="type" onchange="newDoc(this.value)"> 
     <option>Select Category</option> 

     <?php 
     $res= mysql_query("select * from ".CATEGORY." order by id asc"); 

     while($data=mysql_fetch_array($res)) 
     { 
     ?> 
      <option value="<?php echo $data['id'];?>"><?php echo $data['cat_name'];?></option> 
     <?php 
     } 
     ?> 

    </select> 
</div> 
<div class="form-group"> 
    <label for="exampleInputEmail1">Subcategory Name</label> 
    <select name="type1" id="type1"> 
     <option>Select Category</option> 
     <?php 
     $res= mysql_query("select * from ".SUBCATEGORY." where SUB_ID='$id' order by id asc"); 
     while($data=mysql_fetch_array($res)) 
     { 
     ?> 
     <option value="<?php echo $data['id'];?>"><?php echo $data['sub_name'];?></option> 
     <?php 
     } 
     ?> 
    </select> 
</div>