ajax checkbox filter

Posted by user1018298 on Stack Overflow See other posts from Stack Overflow or by user1018298
Published on 2011-11-15T09:27:17Z Indexed on 2011/11/15 9:50 UTC
Read the original article Hit count: 209

Filed under:
|
|

I need some help with a checkbox filter I am working in. I have four groups of checkboxes (type, vehicle, availability, price) and I would like to filter the content on the page based on the user's input. I can get the filter working OK but I am having issues with the groups. I can only seem to match one checkbox from each group rather than all checkboxes from each group.

For example, a visitor can select 1 option in group 1, all options in group 2, 1 option in group three and 1 option in group 4.

I am using ajax to build an SLQ query to return the correct filtered results from my database.

here is my code:

 $('div.filters').delegate('input:checkbox', 'change', function() {
   var type = $('input.exp_type:checked').map(function ()
   {
       return $(this).attr('value');
   }).get().join(',');
   var vehicle = $('input.vehicle_type:checked').map(function ()
   {
       return $(this).attr('value');
   }).get().join(',');
   var avail = $('input.availability:checked').map(function ()
   {
       return $(this).attr('value');
   }).get().join(',');
   var price = $('input.price:checked').map(function ()
   {
       return $(this).attr('value');
   }).get().join(',');
   //alert($options);
   $.ajax({
        type:"POST",
        url:"filtervouchers.php",
        data:"type="+type+"&vehicle="+vehicle+"&avail="+avail+"&price="+price,
        success:function(data){
                $('.results').html(data);
        }


    });//end ajax

})

and the php code:

$type = mysql_real_escape_string($_POST['type']);
$vehicles = mysql_real_escape_string($_POST['vehicle']);
$avail = mysql_real_escape_string($_POST['avail']);
$price = mysql_real_escape_string($_POST['price']);



if ($type != '')
   {
    $sql2 = " AND exp_type IN ($type)";
    }
    if ($vehicles != '')
    {
        $sql3 = " AND vehicle_type LIKE '%$vehicles%'";
    }
    if ($avail != '')
    {
    $sql4 = " AND availability LIKE '%,' $avail% ','";
    }
    if ($price != '')
    {
    $sql5 = " AND price_band IN ($price)";
    }

Can this be done using jquery?

© Stack Overflow or respective owner

Related posts about AJAX

Related posts about checkbox