I'm trying to write the geolocation data to an HTML file, but doesn't work. I always get the following message regarding data:
"Notice: Undefined index: "
The jquery variable data is always empty
... what am I doing wrong?
Thanks in advance.
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<body onload="testResults()">
<script>
function testResults()
{
if (navigator.geolocation)
{
//Get the current position
navigator.geolocation.getCurrentPosition(function (position)
{
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
//var cadena = "lat=" + latitude + "&lon=" + longitude;
var cad = latitude + " " + longitude;
// do callback
$.ajax(
{
type: "POST",
url: "checklo.php",
//data: "lat=" + latitude + "&lon=" + longitude
data: cad,
}).done(function (msg)
{
// Only to check it
alert("Data Saved: " + cad);
});
});
}
else
{
alert("Sorry... your browser does not support the HTML5 GeoLocation API");
}
}
and the code of php file checklo.php
<?php
$file = 'geo.html';
$datos = $_POST['data'];
if (isset($datos)) {
$fp = fopen($file, "a");
fputs($fp, "</br>$datos </br>");
flock($fp, 3);
fclose($fp);
//echo 'msg ';
}
else {
//echo 'msg !';
}
?>