icarus
icarus

Reputation: 137

jQuery.post() - Trouble sending post data

I have a html file with the following javascript code to call the jquery.post function and post some data to test.php

<script type="text/javascript">
      $.post("test.php", { name: "John", time: "2pm" }, function(data) {
         alert("Data Loaded: " + data);
       });
</script>

test.php is as follows

<?php
   echo "Name: ".$POST['name'];
?>

Unfortunately, my alert only shows "name: " without sending back the post data.

Using firebug, however, I can see that the post data is in fact being sent. So I'm very confused as to why $POST isn't working in my php file.

Upvotes: 5

Views: 8496

Answers (3)

Nhu Trinh
Nhu Trinh

Reputation: 13956

its $_POST['variable'] not $POST check your php syntax http://php.net/manual/en/reserved.variables.post.php

Upvotes: 1

Stanley
Stanley

Reputation: 5127

The javascript function is fine. The problem is at the server side. You should write $_POST, not $POST.

echo "Name: ".$POST['name'];

Upvotes: 5

Gravy
Gravy

Reputation: 12445

Your data is being stored in an array like structure...

As such, you should alert the data as follows:

alert("Data Loaded: Name=" + data('name') + " Time="+ data('time'));

Also, there is a typo in your php

echo "Name: ".$_POST['name'];

You need the _. e.g. not $POST, but $_POST

Upvotes: 2

Related Questions