0

How does php access a variable passed by JQuery?

I have the following code:

$.post("getLatLong.php", { latitude: 500000},
   function(data){
     alert("Data Loaded: " + data);
   });

but I don't understand how to access this value using php.

I've tried

$userLat=$_GET["latitude"]; 

and

$userLat=$_GET[latitude];

and then I try to print out the value (to check that it worked) using:

echo $userLat; but it does not return anything. I can't figure out how to access what is being passed to it.

1
  • 1
    $.get(..) > $_GET[..] | $.post(..) > $_POST[..] :) Commented Jan 14, 2011 at 23:48

3 Answers 3

5

$_GET is for url variables (?latitude=bla), what you want is ..

$_POST['latitude']
Sign up to request clarification or add additional context in comments.

Comments

2

use $_POST['latitude'] instead of $_GET.

the reason of course is that your jQuery call is to $.post, which transfers your latitude via the post method.

Comments

2

You're using $.post. That means it will be in the $_POST superglobal in PHP.

If you were using $.get, it would be sent to PHP in the query string, and be in the $_GET superglobal array.

If you want to access it without worrying which HTTP method you used in jQuery, use $_REQUEST.

Try var_dump($_REQUEST); or var_dump($_POST); in your PHP page, and look at the data that comes back using Firebug or the webkit Inspector.

Comments

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.