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

时间:2014-10-08 09:16:27

标签: 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">
        <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>                               

4 个答案:

答案 0 :(得分:6)

在使用jquery时,如果选择了一个类别,它将获取值并从ajax值发布到您的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>";
        }
?>

这将起作用

答案 1 :(得分:0)

您的查询错误从表名中删除引号

$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子句。

答案 2 :(得分:0)

您需要在onCategoryClick上执行AJAX请求,或者使用属于类别的子类别数组生成java代码,并在js中切换它们。 https://stackoverflow.com/a/11238038/3711660 - 关于AJAX请求

答案 3 :(得分:0)

试试这个可以帮助你...

<?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>