0

Forgive my ignorance, but I can't figure out how to do a simple ajax request in PHP. I understand the principles of client-server communication in other languages/libraries, but PHP eludes me for some reason.

Here's what I'm trying to do:

Send a request from here, utilizing jQuery:

$(document).on('click', '#uploadAll', function() {
    $.ajax({
        url: '/ExamplePhpFile.php',
        type: "POST",
        data: {query:'INSERT INTO Assets SELECT * FROM Uploads'},
        success: function(respose){
            console.log("POST successful");
        }
    });
});

Get the query string in the data attribute here:

if(isset($_POST{'data'})) {
    $query = $_POST['data'];
    foreach($query as $value) {
        echo 'here is your crap: '.$value;
    }
}

Then I will use the passed query in another function to run an action on the server (this part works). Obviously I have something wrong, as the $_POST['data'] bit doesn't return a value. And monitoring $_POST by itself just gives me an array object (JSON_encode() doesn't do the trick on that either)

Can anyone offer me guidance, and save me some hair pulling? :)

15
  • 4
    Also it is a REALLY bad security vulnerability to send SQL statements across web requests. Commented May 24, 2018 at 17:42
  • I forgot them in the post, they're there in the file. I'll edit to reflect :) Commented May 24, 2018 at 17:43
  • Also your parameters will not have data as a key. It will have query as a key. $_POST['query'] Commented May 24, 2018 at 17:44
  • Turns out you can actually use the {} as in isset($post{'data'}) ... lol who knew. Commented May 24, 2018 at 17:48
  • @ArtisticPhoenix As I just pointed out, data will not be a key in the $_POST associative array Commented May 24, 2018 at 17:49

2 Answers 2

1

You are passing your data key as query and using $_POST['data'] so how it will work replace like $_POST['data'] to $_POST['query'] it will work.

Sign up to request clarification or add additional context in comments.

5 Comments

hmmm, I tried that, and when I echo it, i get nothing :(
Show us var_dump($_POST) output
its empty? this confuses me
Try echoing file_get_contents('php://input') to see what the raw HTTP request body is. Might be some clues in there.
That's empty too, something appears to be wrong with the client side of things?
1

You need to send a valid JSON object back

if(isset($_POST['query'])) {
    $query = $_POST['query'];
    $data = [];
    $x = 0;
    foreach($query as $value) {
        $data[$x] = 'here is your crap: '.$value;
        $x++;
    }
    print(json_encode($data));
}

4 Comments

What difference do you think that makes? He clearly said he is not getting a response, it's because he's not returning a valid JSON object. Maybe read the actual question and the response before downvoting everyone? Just a thought
He also clearly said his queries work, it's the response to the AJAX call he isn't getting.
What part of the issue do you think it isn't addressing? He wants to know how to get a response from the server when the AJAX call is done.
Now I see where you were going with that. Sorry, long day.

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.