[英]PHP file not getting variable sent via AJAX using POST
I'm trying to send a jquery
variable to a php
script as part of a search feature I'm building for a website. 我正在尝试将
jquery
变量发送到php
脚本,这是我正在为网站构建的搜索功能的一部分。 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: 我希望使用
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);
}
});
The above is working, however I'm running into trouble with the response I get back from the new-search-get-results.php
file. 以上工作正常,但是我从
new-search-get-results.php
文件中new-search-get-results.php
的响应遇到了麻烦。 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'];
它在
php
文件中涉及的行是: $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: 这是我的
new-search-get-results.php
文件:
$keyword = $_POST['keyword'];
echo $keyword;
change 更改
data: JSON.stringify({keyword: $keyword}),
to 至
data: {keyword: $keyword},
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.