0

I have this: A javascript file that does a request to a php file.

I have two variables called Lat and Lng in my javascript. When I do the request to the php file with ajax I would like those two variables to be translated in to php values that the php file will use.

I hope this explains it clear enough. How can I do this?

Code: Javascript

var lng = center.lng();
var lat = center.lat();
$.ajax({
    url: "ajax.php"
}).done(function(data){};

Code: Php

$lat = '';
$lng = '';

What I want is those two variable values from the Javascript in the php with each request.

If it helps to know what I'm doing exactly: I'm getting the longtitude and latitude from the google maps center. These values I want to use in the file_get_contents url in my php. Every time the request happens the url gets dynamic because the values will be send with the request.

1
  • Can you please post the code that you are currently using? As it currently stands it's not too clear what the problem is. Commented Mar 12, 2014 at 15:06

2 Answers 2

1

Script to request php file through POST data:

<script type='text/javascript'>
var longitude = center.lng();
var latitude = center.lat();
$.ajax({
    type: 'POST',
    url: "ajax.php",
    data:{longitude:longitude, latitude:latitude},
    success: function(){
        alert('Success');
    }
}).done(function(data){console.log(data);};
</script>

And in PHP file: you can get values as:

$longitude = $_POST['longitude'];
$latitude = $_POST['latitude'];

Hope this helps!!!

Sign up to request clarification or add additional context in comments.

Comments

0

you need to send the variables through your ajax request first of all. I use something like this

{lng:lng,lat:lat},

and then you make a request in php something like this

if ($_REQUEST['lng']) {
    $lng = $_POST['lng'];
    $lat = $_POST['lat']
} 

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.