I tried to send values from JavaScript to PHP by following the code provided at
Send date from JavaScript to PHP without using Query String
and I am getting error
Notice: Undefined index: getlat in /Applications/XAMPP/xamppfiles/htdocs/test1.php on line 25
Notice: Undefined index: getlon in /Applications/XAMPP/xamppfiles/htdocs/test1.php on line 26
Can anyone help me fixing the issue, thanks.
My code is:
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.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:"test1.php",type:"POST",async:false,
data:{getlat:$("#getlat").val(),getlon:$("#getlon").val()}
});
});
</script>
</head>
<body>
<input type="hidden" id="getlat" name="getlat" />
<input type="hidden" id="getlon" name="getlon" />
<?php
$lat = $_POST["getlat"];
$lon = $_POST["getlon"];
echo $lat;
echo $lon;
?>
</body>
</html>
UPDATE 1
Following the comments I tried this way and that too didn't work
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.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;
}
</script>
</head>
<body>
<input type="hidden" id="getlat" name="getlat" />
<input type="hidden" id="getlon" name="getlon" />
<?php
$lat = $_POST["getlat"];
$lon = $_POST["getlon"];
echo $lat;
echo $lon;
?>
<script>
$( document ).ready(function() {
getLocation();
$.ajax({url:"test1.php",type:"POST",async:false,
data:{getlat:$("#getlat").val(),getlon:$("#getlon").val()}
});
});
</script>
</body>
</html>
getLocation()?$_POST["getlat"]and$_POST["getlon"]further down the page? If so this wont work, that is not how ajax works