djq
djq

Reputation: 15286

Accessing a variable passed by jquery using php

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.

Upvotes: 0

Views: 212

Answers (3)

JAL
JAL

Reputation: 21563

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.

Upvotes: 2

davin
davin

Reputation: 45525

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.

Upvotes: 2

Naatan
Naatan

Reputation: 3444

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

$_POST['latitude']

Upvotes: 5

Related Questions