amit
amit

Reputation: 10261

receiving data in php through jquery ajax

i just got the hang of using jquery ajax for example, this code sends the contents of the comment box to the php file.

    $.post("user_submit.php", {
      comment: $("#comment").text()
    });

the question however is how do i receive the data in the user_submit.php file?

Upvotes: 1

Views: 1285

Answers (4)

cletus
cletus

Reputation: 625397

With $.post() any data sent like this:

$.post("user_submit.php", {
  comment: $("#comment").text()
});

is received as such:

<?php
$comment = $_POST['comment'];
...
?>

Upvotes: 1

Andreas Bonini
Andreas Bonini

Reputation: 44832

If you mean "how do I receive the output of user_submit.php", then the solution is to use the callback parameter:

$.post("user_submit.php", { comment: $("#comment").text() },
  function(data){
    alert("Data Loaded: " + data);
  });

If you mean "how do I receive the comment in user_submit.php", then you should use:

htmlspecialchars($_POST["comment"]);

Links to php manual pages for htmlspecialchars, $_POST.

Upvotes: 0

nickf
nickf

Reputation: 546473

It will be in the $_POST array:

print_r($_POST);

...this will show you everything posted to that page.

Upvotes: 1

Pekka
Pekka

Reputation: 449783

The basic usage in the PHP side is:

echo $_POST["comment"]; // unsafe

Remember basic security like escaping:

echo htmlspecialchars($_POST["comment"]); // minimum    

Upvotes: 1

Related Questions