繁体   English   中英

PHP文件未获取使用POST通过AJAX发送的变量

[英]PHP file not getting variable sent via AJAX using POST

我正在尝试将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文件中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},

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM