I'm trying to send a jquery variable to a php script as part of a search feature I'm building for a website. I wish to use AJAX to perform the request for the php file and so far I've gotten this as my new-search.js script:
$('#bt_search').click(function(){
$keyword = $('#keyword').val();//get the keyword from the input box
$contentArray = []; //Hold checked "content" filters
$typeArray = []; //Hold checked "type" filters
$locationArray = []; //Hold checked "location" filters
//Content Filter - cycle through each filter and add value of checked ones to array
$('.content-filter :checked').each(function(){
$contentArray.push(this.value);
})
//Type Filter
$('.type-filter :checked').each(function(){
$typeArray.push(this.value);
})
//Location Filter
$('.location-filter :checked').each(function(){
$locationArray.push(this.value);
})
//Testing
console.log("Keyword: " + $keyword);
console.log("Content Filters: " + $contentArray);
console.log("Type Filters: " + $typeArray);
console.log("Location Filters: " + $locationArray);
/*
* Make AJAX Request to "new-search-get-results.php", passing
* keyword and filter arrays to the requested file.
*
*/
$.ajax({
url: "../pages/ajax/new-search-get-results.php",
data: JSON.stringify({keyword: $keyword}),
type: "POST",
success: function(response){
console.log(response);
}
});
The above is working, however I'm running into trouble with the response I get back from the new-search-get-results.php file. This is the error:
( ! ) Notice: Undefined index: keyword in C:\wamp\www\mysite.tld\pages\ajax\new-search-get-results.php on line 6
The line it relates to in the php file is: $keyword = $_POST['keyword'];
Does anyone know where I am going wrong so I can fix this error? This is my new-search-get-results.php file:
$keyword = $_POST['keyword'];
echo $keyword;
var_dump($_POST);and see whats in therejson_decode()your post-variable in order you send a json-string to your php-filejson_decode(). besides this, it still would not work since the $_POST['keyword'] variable would still not be set