When I call ajax via some search or filter functionality I’ve written in, the full list is improperly returned without the filters. I’ve been able isolate so far to find that the data in the ajax call is not reaching the PHP $_GET variable. Below is the jQuery capturing data for and creating the ajax call that runs initially on page load and when a user searches or filters:
jQuery(function($) {
var attempt = 1;
var scholarship_search;
var scholarship_region;
var 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);
});
And the following is the PHP script that should capture the request and returns the page data, but for some reason the ajax $.get(); is not reaching the 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
} ?>
Any ideas?
UPDATE 2/25/16
Got it solved, guys. the queryData
variable needed to be broken down into key.value pairs in order to be sent and picked up. i.e. `data : { scholarship_search : queryData.value } etc…
Thanks for your input, everyone.
The query data for the ajax call from search, for example, should be
The added quotes are the important part. Then when you call
the php script will receive the
$_GET
variables you intend.You try to pass
scholarship_filter_submit
andscholarship_search_submit
as booleans but they will reach server as strings – ‘true’ or ‘false’. So you cannot check their values later in php asif($search_submit)
andelseif($filter_submit) {
. You should useif($search_submit==='true')
andelseif($filter_submit === 'true') {
.Check out chrome dev tools or use fiddler to see your network traffic. This will be step one to make sure your Ajax request URL is correctly formatted.
The issue is in the way the data is sent to ajax.
queryData.value1
and so on should be replaced with new variable names you give to each of the items.