This problem has already been addressed in other posts and other forums around the network but i don't understand where wrong.
I've this code javascript for find user's position:
if(!!navigator.geolocation){
navigator.geolocation.getCurrentPosition(geolocalizzami);
}
else {
alert("Geolocalizzazione non supportata");
}
function geolocalizzami(position) {
document.getElementById("lon").innerHTML = position.coords.longitude;
document.getElementById("lat").innerHTML = position.coords.latitude;
}
and i can print with
<label id='lon' /></label>
and ok. I need of loading these data into database. I know I need to use jquery and ajax. The ajax code into an html page its serv me for find the coordinates and send these data, with the post method, to php page. here i can using $_POST['value'] for work with the data. An example, found in internet, that i use is:
page.html:
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="http://maps.google.com/maps/api/js?sensor=true"></script>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js">
</script>
</head>
<body>
<script type="text/javascript">
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
$.ajax({
url:'page2.php',
data: {
'lat': position.coords.latitude,
'lng': position.coords.longitude
},
type: 'get',
success: function (result) {
// If your backend page sends something back
alert(result);
}
});
});
}
</script>
</body>
</html>
page2.php:
<?php
echo $lat = $_POST['lat'];
echo $lng = $_POST['lng'];
?>
Why $lat and $lng are empty? thanks in advance.