Reputation: 817
I have the following javascript function
function success_callback(p)
{
lat = p.coords.latitude.toFixed(2);
lon = p.coords.longitude.toFixed(2);
}
Now I want to transfer both the variable to PHP using Jquery AJAX, I am pretty new to Jquery, I am not able to figure out how to do it. I want to transfer the variables to the same PHP file where this JS code resides. Is that possible ?
Upvotes: 0
Views: 3015
Reputation: 23053
Yes it is. You could post the variables using the data string. Have a look at the Manual.
$.ajax({
type: "POST",
data: "lat="+lat+"&lon="+lon,
success: function(){
//callback code
alert("Done!");
}
});
Upvotes: 2
Reputation: 1170
javascript:
$.get('index.php?lat='+ lat + '&long=' + lon)
php:
$lat = isset($_GET['lat']) ? $_GET['lat'] : 0;
$lon = isset($_GET['lat']) ? $_GET['long'] : 0;
If your current page is named index.php.
Keep in mind the current page is going to process again unless you specifically program your php not to. You asked to send it to the current page, though, so that is what this does.
Upvotes: 0
Reputation: 50029
You could use jQuery.get(). The syntax is easy
function success_callback(p) {
lat = p.coords.latitude.toFixed(2);
lon = p.coords.longitude.toFixed(2);
var coords = {
lat: lat,
long: lon
};
$.get('mypage.php', coords, function () {
alert('data sent');
});
}
And in your PHP script, you use the $_GET
$lat = isset($_GET['lat']) ? $_GET['lat'] : 0;
$long = isset($_GET['lat']) ? $_GET['long'] : 0;
Upvotes: 0
Reputation: 1008
using ajax call, you can send values to another php file, in case of same file needs to use condition needs to be checked.but best is to pass parameters to another file for pressing. Where you wanted to use/why you wants those on same page?
Upvotes: 0