Search code examples
phpjqueryajaxcodeigniter

Filtering data according to click of checkboxes


Currently I'm displaying all my data in a table format in my View class. Now I have checkboxes on top which are default checked when the user enters the page. Now I want it so that when the user unchecks any of them, it should send a POST ajax call to my controller and removing that filter from my model. To do this I'm using the following code:

View Class:

<span><input type="checkbox" id="propsubcommunity" name="matchfield" value="propsubcommunity" checked>
<label for="propsubcommunity">Sub-Community</label></span>
<span><input type="checkbox" id="community" name="matchfield" value="community" checked>
<label for="community">Community</label></span>
<span><input type="checkbox" id="proptype" name="matchfield" value="unit_type" checked>
<label for="proptype">Type (Villa, Apartment,etc)</label></span>
<span><input type="checkbox" id="propbeds" name="matchfield" value="bedrooms" checked>
<label for="propbeds">Beds</label></span>

<?php if($properties) foreach($properties as $lead): ?>
<td><?php echo $lead['refno'] ?></td>
<?php endforeach;?>

<script>
$("input[name='matchfield']").change(function() {
    var matchfields = [];
    $.each($("input[name='matchfield']:checked"), function(){
        matchfields.push($(this).val());
    }); 
     $.ajax({
         url: 'listings/edit/'+<?php echo $property->listingsid;?>,
         data : { selmatches : matchfields, clickmatches:'clicked' },
         type: 'POST',
         beforeSend: function(){
            $("#matchedleadscont").empty();
            $("#loader").show();
         },             
         success: function (data) {
          $("#loader").hide();
            $("#matchedleadscont").html(data);
          }
    }); 
});

Controller Class:

if(isset($_POST["clickmatches"]) ){
                if(in_array('community', $_POST["selmatches"])){
                    $propcommunity = $default_data[0]['community'];
                }
                if(in_array('propsubcommunity', $_POST["selmatches"])){
                    $propsubcommunity = $default_data[0]['property_name'];
                }
                if(in_array('bedrooms', $_POST["selmatches"])){
                    $propbeds = $default_data[0]['bedrooms'] ;
                }
                if(in_array('unit_type', $_POST["selmatches"])){
                    $proptype = $default_data[0]['unit_type'];
                }
            $edit['properties']=  $this->listings_model->load_leads($propcommunity, $propsubcommunity,$propbeds,$proptype);
                }

Model Class:

$this->db->select('*');
$this->db->from("table1");
if($community!='')
           $this->db->where('crm_data_table.community',$community); 
if($subcommunity!='')
           $this->db->where('crm_data_table.property_name',$subcommunity); 
if($proptype!='')
           $this->db->where('crm_data_table.unit_type',$proptype);
if($bedrooms!='')
           $this->db->where('crm_data_table.bedrooms',$bedrooms);
$query = $this->db->get();  
return $query->result_array();

But this doesn't change when I change my checkboxes. I put a var_dump($_POST["selmatches"]); die(); and "clickmatches" and both gave the following:

enter image description here


Solution

  • We can simplify the jQuery

    $("input[name='matchfield']").on("change", function() {
      const matchfields = $("input[name='matchfield']:checked").map(function() {
        return $(this).val()
      }).get();
    
      const data = {
        selmatches: matchfields,
        clickmatches: 'clicked'
      };
      $.ajax({
         url: 'listings/edit/'+<?php echo $property->listingsid;?>,
         data : data,
         ...
      });
    });
    

    The code below gets

    array(4) { [0]=> string(16) "propsubcommunity" [1]=> string(9) "community" [2]=> string(9) "unit_type" [3]=> string(8) "bedrooms" }
    found community (if community is on)
    

    PHP

    <?php 
    
    error_reporting(E_ALL);
    header("Access-Control-Allow-Origin: *"); // if on another server
    
    $selmatches = $_POST['selmatches'];
    
    var_dump($selmatches);
    if(in_array('community', $_POST["selmatches"])){
      echo "Found community";
    }
    
    ?>
    

    $("input[name='matchfield']").on("change", function() {
      const matchfields = $("input[name='matchfield']:checked").map(function() {
        return $(this).val()
      }).get();
    
      const data = {
        selmatches: matchfields,
        clickmatches: 'clicked'
      };
      console.log(data)
      $.ajax({
        url: 'https://plungjan.name/SO/testAjaxArray/save.php',
        data: data,
        type: 'POST',
        success: function(data) {
          $("#matchedleadscont").html(data);
        }
      });
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <span><input type="checkbox" id="propsubcommunity" name="matchfield" value="propsubcommunity" checked>
    <label for="propsubcommunity">Sub-Community</label></span>
    <span><input type="checkbox" id="community" name="matchfield" value="community" checked>
    <label for="community">Community</label></span>
    <span><input type="checkbox" id="proptype" name="matchfield" value="unit_type" checked>
    <label for="proptype">Type (Villa, Apartment,etc)</label></span>
    <span><input type="checkbox" id="propbeds" name="matchfield" value="bedrooms" checked>
    <label for="propbeds">Beds</label></span>
    <div id="matchedleadscont"></div>