I don't know why my code is not working, I am trying to send the coordinates from JavaScript to PHP using AJAX and I am able to pull values from JavaScript to textbox but values are not passed to PHP. Any help is highly appreciated.
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-2.1.0.min.js"></script>
<script>
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
}
}
function showPosition(position) {
document.getElementById("getlat").value = position.coords.latitude;
document.getElementById("getlon").value = position.coords.longitude;
}
$( document ).ready(function() {
$.ajax({url:"samepage3.php",type:"POST",async:false,
data:{getlat:$("#getlat").val(),getlon:$("#getlon").val()}
});
});
</script>
</head>
<body onload="getLocation()">
<input type="text" id="getlat" name="getlat" />
<input type="text" id="getlon" name="getlon" />
<?php
if(isset($_POST["getlat"]))
{
$lat = $_POST["getlat"];
echo $lat;
}
if(isset($_POST["getlon"]))
{
$lon = $_POST["getlon"];
echo $lon;
}
?>
</body>
</html>
UPDATE 1:
I am running this code in file samepage3.php and I need the action to be happened on the same page without reloading the page
data:{'getlat':$("#getlat").val(),'getlon':$("#getlon").val()