0

I'm trying to send a jquery variable to a php script as part of a search feature I'm building for a website. 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:

$('#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. 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'];

Does anyone know where I am going wrong so I can fix this error? This is my new-search-get-results.php file:

$keyword = $_POST['keyword'];
echo $keyword;
4
  • 3
    You dont need to stryingify. You must not stringify. Try var_dump($_POST); and see whats in there Commented Nov 14, 2014 at 11:16
  • you need to json_decode() your post-variable in order you send a json-string to your php-file Commented Nov 14, 2014 at 11:16
  • Yes, I just discovered that there...I don't know why I was doing that Commented Nov 14, 2014 at 11:16
  • 2
    @empiric no, he doesn't and it's json_decode(). besides this, it still would not work since the $_POST['keyword'] variable would still not be set Commented Nov 14, 2014 at 11:17

1 Answer 1

5

change

data: JSON.stringify({keyword: $keyword}),

to

data: {keyword: $keyword},
Sign up to request clarification or add additional context in comments.

1 Comment

glad to know it's helpful for you. you can accept helpful answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.