0

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 ?

0

4 Answers 4

2

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!");
  }
});
Sign up to request clarification or add additional context in comments.

Comments

0

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?

Comments

0

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;

Comments

0

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.

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.