Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Adam Duffield
Adam Duffield
30,494 Points

Simple AJAX PHP Post request?

Hi there coders of treehouse!

I'm fairly experienced in quite a lot after about 10 months of constant coding and time with treehouse but I'm struggling to find a solid example out there for what I'm after.

E.g. All i want to be able to do is POST the value of a searchbox ONKEYUP to a seperate PHP file that can store the POSTED variable.

I would give you some example code but in my frustration i scrapped it all and thought I would try again some other time.

I can't use a GET method at all for this and I can get AJAX to send a request and get a simple echo from the PHP file but whatever i put into the input won't store into a variable when posted with AJAX. I thought i would put this question under PHP because it's the post/variable thats the problem not so much the Javascript.

Many thanks,

Adam

1 Answer

Casey Ydenberg
Casey Ydenberg
15,622 Points

Your code would be helpful, but ...

Debugging AJAX can be a bit of a pain. What I try is usually something like:

    <?php
        echo json_encode($_POST);
    ?>

You didn't talk about the Javascript code that's making the request, but make sure the request method is POST and then console.log the response. In jQuery this would be

    $.ajax({
        method: "POST",
        dataType: "JSON",
        success: function(data) {
            console.log(data);
        }
    }

If you're PHP is firing back what you send to it, all you need to do is do something with that data.