我正在尝试将jquery
变量发送到php
脚本,作为我为网站构建的搜索功能的一部分。我希望使用 AJAX
来执行对 php
文件的请求,到目前为止,我已经将其作为我的new-search.js
脚本:
$('#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);
}
});
以上方法有效,但是我从new-search-get-results.php
文件中返回的响应遇到了麻烦。这是错误:
( ! ) Notice: Undefined index: keyword in C:'wamp'www'mysite.tld'pages'ajax'new-search-get-results.php on line 6
它在php
文件中相关的行是:$keyword = $_POST['keyword'];
有谁知道我哪里出了问题,以便我可以修复此错误?这是我new-search-get-results.php
文件:
$keyword = $_POST['keyword'];
echo $keyword;
更改
data: JSON.stringify({keyword: $keyword}),
自
data: {keyword: $keyword},