gpt4 book ai didi

php - 如何显示属于一个类别的子类别?

转载 作者:行者123 更新时间:2023-12-04 00:18:40 25 4
gpt4 key购买 nike

此代码适用于类别和子类别。在类别中我显示下拉列表中的所有值但对于子类别我想显示属于类别的确切子类别。请帮助我

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

最佳答案

在选择类别时使用 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>

在同一个目录下新建一个名为ajax.php的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>";
}
?>

这会起作用

关于php - 如何显示属于一个类别的子类别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26253261/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com