我正在尝试将一个jquery变量发送到一个php脚本,作为我为一个网站构建的搜索功能的一部分。我希望使用AJAX来执行对php文件的请求,到目前为止,我已经将其作为php脚本获得:
$('#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;发布于 2014-11-14 11:16:54
变化
data: JSON.stringify({keyword: $keyword}),至
data: {keyword: $keyword},https://stackoverflow.com/questions/26928713
复制相似问题