0
我需要發送帶有複選框值的ajax請求(每次點擊/檢查)。我有一個複選框三個列表,每個列表過濾數據returend從PHP(與MySQL和條件):ajax根據複選框值發送數據
$('body').on('click', '.click, :checkbox, .pag_link', function() {
var self = this;
// radio buttons
if ($('#res_prop').is(':checked')) {
var use = $('#res_prop').val();
}
else if ($('#com_prop').is(':checked')) {
var use = $('#com_prop').val();
}
else {
$('p.error').show();
die();
}
//checkboxes from each list have the same class, is this ok?
if ($(self).is(':checkbox')) {
$(self).on('change', function() {
if ($(self).prop('class') == 'filter1' || $('.filter1').is(':checked')) {
if ($('.filter1').is(':checked'))
var type = $(self).val(); // maybe should be an array
else var type = null;
} else var type = null;
if ($(self).prop('class') == 'filter2' || $('.filter2').is(':checked')) {
if ($('.filter2').is(':checked'))
var status = $(self).val(); // maybe should be an array
else var status = null;
} else var status = null;
if ($(self).prop('class') == 'filter3' || $('.filter3').is(':checked')) {
if ($('.filter3').is(':checked'))
var bhk = $(self).val(); // maybe should be an array
else var bhk = null;
} else var bhk = null;
});
}
else {
var type = status = bhk = null;
}
if ($(self).is('.pag_link')) {
if ($(self).text() == '«')
var page = (parseInt($('.active').text()) - 1);
else if ($(self).text() == '»')
var page = (parseInt($('.active').text()) + 1);
else
var page = parseInt($(self).text());
}
else {
var page = 1;
}
$.ajax({
method: 'POST',
url: '/search',
data: {
'do': getUrlParameter('do'),
'use': use,
'type': type,
'status': status,
'bhk': bhk,
'city': $('select[name="city"]').val(),
'zone': $('select[name="zone"]').val(),
'page': page
}
}).done(function(data) {
if ($('#search').is(':visible'))
$('#search').hide();
if ($(self).is(':checkbox')) {
alert('should work');
var new_content = $(data).find('#scroll-to-list');
$('#scroll-to-list').replaceWith(new_content);
}
else {
var new_content = $(data).find('#search-filters, #scroll-to-list');
$('#results').html(new_content);
$('html, body').animate({
scrollTop: $('#scroll-to-list').offset().top
}, 1000);
}
});
});
我期待每次點擊/檢查做Ajax請求,並打印新的結果(依靠在服務器上是性能方面?)。如果選中,變量將獲取該值併發送,如果未選中,變量將爲空。 search.php腳本處理mysql選擇查詢(ifs將AND條件添加到查詢中)。
我剛剛在數據庫中有1個例子,但checkboxes腳本不工作(或mysql選擇查詢不包括和條件,我認爲問題是在jQuery中,php腳本是好的)。
$use = isset($_POST['use']) ? (int) $_POST['use'] : ''; // int AJAX
$filter_type = isset($_POST['type']) ? (int) $_POST['type'] : ''; // int AJAX
$filter_status = isset($_POST['status']) ? (int) $_POST['status'] : ''; // int AJAX
$filter_bhk = isset($_POST['bhk']) ? (int) $_POST['bhk'] : ''; // int AJAX
$filter_city = isset($_POST['city']) ? (int) $_POST['city'] : 0; // int AJAX
$filter_zone = isset($_POST['zone']) ? (int) $_POST['zone'] : 0; // int AJAX
$page_number = isset($_POST['page']) ? (int) $_POST['page'] : ''; // int AJAX
if ($filter_type != NULL || $filter_type != '') {
$filter_type = 'AND t2.type = ' . $filter_type;
} else $filter_type = NULL;
if ($filter_status != NULL || $filter_status != '') {
$filter_status = 'AND t2.status = ' . $filter_status;
} else $filter_status = NULL;
if ($filter_bhk != NULL || $filter_bhk != '') {
$filter_bhk = 'AND t2.bhk = ' . $filter_bhk;
} else $filter_bhk = NULL;
if ($filter_city > 0) {
$filter_city = 'AND t2.city = ' . $filter_city;
$filter_zone = NULL;
if ($filter_zone > 0) {
$filter_zone = 'AND t2.zone = ' . $filter_zone;
}
} else $filter_city = $filter_zone = NULL;
if ($stmt = $mysqli->prepare(' SELECT t1.id, t2.*
FROM ' . $table . ' t1 // not from get/post
INNER JOIN property t2 ON t2.id = t1.id
WHERE t2.use = ?
' . $filter_type
. $filter_status
. $filter_bhk
. $filter_city
. $filter_zone . '
LIMIT ?, ?')) {
$stmt->bind_param('iii', $use, $start, $limit);
$stmt->execute();