[英]How can I obtain which checklist items are checked in php and use them to make a sql query?
I'm trying to create a checklist that acts as a filter for my database. 我正在尝试创建一个清单,作为我的数据库的过滤器。 The checklist would be use to enter multiple values to search for. 该清单将用于输入要搜索的多个值。 For example, the user should be able to search for the brands AC and Abarth at the same time. 例如,用户应该能够同时搜索AC和Abarth品牌。
This is what the checklist for brands looks like. 这就是品牌清单的样子。
<h3>Brand</h3>
<?php
$query = "select distinct(Brand) from Sheet1 order by Brand ASC";
$rs = mysqli_query($conn,$query) or die("Error : ".mysql_error());
while($brand_data = mysqli_fetch_assoc($rs)){
?>
<a href="javascript:void(0);" class="list-group-item">
<input type="checkbox" class="item_filter brand" value="<?php echo $brand_data['Brand']; ?>" >
<?php echo $brand_data['Brand']; ?></a>
<?php } ?>
My ajax code. 我的ajax代码。
var brand;
$(function(){
$('.item_filter').click(function(){
$('.product-data').html('<div id="loaderpro" style="" ></div>');
brand = multiple_values('brand');
$.ajax({
url:"ajax.php",
type:'post',
data:{brand:brand},
success:function(result){
$('.product-data').html(result);
}
});
});
});
function multiple_values(inputclass){
var val = new Array();
$("."+inputclass+":checked").each(function() {
val.push($(this).val());
});
return val;
}
This is how my php handles the checkbox. 这就是我的php处理复选框的方式。
$brand = isset($_REQUEST['Brand'])?$_REQUEST['Brand']:"";
$query = "select * from Sheet1 where";
//filter query start
if(!empty($brand)){
$branddata =implode("','",$brand);
$query .= " Brand in('$branddata')";
}
$rs = mysqli_query($conn,$query) or die("Error : ".mysqli_error($conn))
The result should be $query= select * from Sheet1 where Brand in ('AC', 'Abarth'). 结果应为$ query = select * from Sheet1,其中Brand in('AC','Abarth')。 This is for my database to show the all car models with the brand AC or Abarth. 这是我的数据库,其中显示了AC或Abarth品牌的所有汽车型号。 The current result I'm getting is that it won't give a value of what is checked in the checklist. 我得到的当前结果是,它不会提供清单中所检查内容的值。 So the final result is nothing, it might be because of my ajax code, because $brand doesn't get any value. 所以最终结果什么都没有,可能是因为我的Ajax代码,因为$ brand没有获得任何价值。 The value should be an array that is later converted into a single string. 该值应该是一个数组,该数组以后将转换为单个字符串。
Figured out my own problem. 弄清楚我自己的问题。 The php was wrong. PHP是错误的。 The php is looking for values for the variable Brand, meanwhile the ajax is sending values to the variable brand. php正在寻找变量Brand的值,而ajax会将值发送到变量brand。
Fixed by changing 通过更改固定
$brand = isset($_REQUEST['Brand'])?$_REQUEST['Brand']:"";
to 至
$brand = isset($_REQUEST['brand'])?$_REQUEST['brand']:"";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.