簡體   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