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:
2
Answers
We can simplify the jQuery
The code below gets
PHP
In view:
Change
name="matchfield"
toname="matchfield[]"
….
And in your Controller: