简体   繁体   中英

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.

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']; ?>"  >
                    &nbsp;&nbsp; <?php echo $brand_data['Brand']; ?></a>
                <?php } ?>

My ajax code.

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.

$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'). This is for my database to show the all car models with the brand AC or 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. The value should be an array that is later converted into a single string.

Figured out my own problem. The php was wrong. The php is looking for values for the variable Brand, meanwhile the ajax is sending values to the variable brand.

Fixed by changing

$brand = isset($_REQUEST['Brand'])?$_REQUEST['Brand']:"";

to

$brand = isset($_REQUEST['brand'])?$_REQUEST['brand']:"";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM