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.
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']
}
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!!!