當我通過我寫入的某些搜索或過濾器功能調用ajax時,完整列表被錯誤地返回而沒有過濾器。我已經能夠隔離到目前爲止,發現ajax調用中的數據沒有達到PHP $ _GET變量。下面是jQuery的捕獲數據並創建最初運行在頁面加載,當用戶搜索或過濾器的AJAX調用:
jQuery(function($) {
var attempt = 1;
var scholarship_search;
var \t scholarship_region;
var \t scholarship_state;
var queryData;
var load_posts = function(queryData) {
$.ajax({
type : 'GET', //'POST'
url : '/wp-content/themes/campuspride2015/lib/scholarship-db.php',
data : queryData,
dataType : 'html',
success : function(data, s, o) {
$('#scholarship-results-container').append(data);
console.log(attempt +' : '+ s +' : '+ queryData);
attempt ++;
}
});
}
/**
* ajax call from search
**/
$('#scholarship-search-form').on('submit', function(e) {
e.preventDefault(); // disable the normal click
// gather the query data
sch_search = $('#scholarship-search').val(); //coming from an input field
queryData = {
scholarship_search_submit : true,
scholarship_search : sch_search
};
load_posts(queryData); // ajax page load
});
$('#scholarship-filter-form').on('submit', function(e) {
e.preventDefault(); // disable the normal click
// gather the query data
sch_filter_region = $('#scholarship_region').val(); // select drop down
sch_filter_state = $('#scholarship_state').val(); // select drop down
queryData = {
scholarship_filter_submit : true,
scholarship_region : sch_filter_region
scholarship_state : sch_filter_state
};
load_posts(queryData); // ajax page load
});
load_posts(queryData);
});
而下面是PHP腳本應該捕捉請求並返回頁面數據,但由於某種原因,ajax $ .get();沒有達到php $ _GET。
// WP access
define('WP_USE_THEMES', false);
require_once('../../../../wp-load.php');
// Variables that dictate the results to be shown
$search_submit = (isset($_GET['scholarship_search_submit']) ? $_GET['scholarship_search_submit'] : 0);
$filter_submit = (isset($_GET['scholarship_filter_submit']) ? $_GET['scholarship_filter_submit'] : 0);
/**
* Query for Scholarships
**/
/** Check for search parameters **/
if($search_submit) {
// run query (which is not running...)
}
/** query based on filters **/
elseif($filter_submit) {
// run query (which is also not running...)
}
/** Initial page viewing, show all **/
else {
// this is running, because the $_GET variable is not being populated
} ?>
任何想法?
UPDATE 16年2月25日
得到它解決了,夥計們。需要將queryData
變量分解爲key.value對以便發送和拾取。即`data:{scholarship_search:queryData.value} etc ...
感謝您的輸入,每個人。
做出請求時,頁面加載但'queryData'在這一點上不確定... JS的最後一行'load_posts(queryData);' – charlietfl
權。這正是我的問題。 : -/ – brnatermedia
那麼你期望它在頁面加載時做什麼?如果$ _GET爲空,則設置默認值爲javascript或php ... if(empty($ _ GET)){loadDefaults();} – charlietfl