目前,我在 View 類中以表格格式顯示所有資料。現在我在頂部有復選框,當用戶進入頁面時默認選中這些復選框。現在我想要它,以便當用戶取消選中它們中的任何一個時,它應該向我的控制器發送一個 POST ajax 呼叫并從我的模型中洗掉該過濾器。為此,我使用以下代碼:
查看類:
<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);
}
});
});
控制器類:
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);
}
型號分類:
$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();
但是當我更改復選框時,這不會改變。我放了一個 var_dump($_POST["selmatches"]); 死(); 和“clickmatches”,兩者都給出了以下內容:

uj5u.com熱心網友回復:
我們可以簡化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,
...
});
});
下面的代碼得到
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>
uj5u.com熱心網友回復:
在視圖中:更改name="matchfield"為name="matchfield[]"
....
$("input[type='checkbox']").click(function() {
$.ajax({
url: 'listings/edit/' <?php echo $property->listingsid;?>,
data : $(form).serialize(),
type: 'POST',
beforeSend: function(){
$("#matchedleadscont").empty();
$("#loader").show();
},
success: function (data) {
$("#loader").hide();
$("#matchedleadscont").html(data);
}
});
});
在您的控制器中:
if(in_array('community', $_POST["matchfield"])){
$propcommunity = $default_data[0]['community'];
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/363773.html
