ajax send location after a interval
- By Preneesh AV --
- 21-Mar-2019 --
- 124 Comments
Create a location.php file in web root and past following code:
$req_dump = print_r($_REQUEST, true);
$fp = file_put_contents('formdata.txt', $req_dump, FILE_APPEND);
$fp = file_put_contents('formdata.txt', date("M,d,Y h:i:s A") , FILE_APPEND);
Create a HTML file index.html with following code:
<script src="https://code.jquery.com/jquery-1.11.0.min.js"></script>
<script>function showLocation(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
console.log("Latitude : " + latitude + " Longitude: " + longitude);
$.ajax({
type : 'POST',
url : 'https://location.speaktech.com/location.php',
data : {
lat: position.coords.latitude,
long: position.coords.longitude
},
success : function(data){
setTimeout(update, 15000);
},
});
}
function errorHandler(err) {
if(err.code == 1) {
alert("Error: Access is denied!");
} else if( err.code == 2) {
alert("Error: Position is unavailable!");
}
}
function getLocationUpdate(){
if(navigator.geolocation){
// timeout at 60000 milliseconds (60 seconds)
var options = {timeout:60000};
geoLoc = navigator.geolocation;
watchID = geoLoc.watchPosition(showLocation, errorHandler, options);
} else {
alert("Sorry, browser does not support geolocation!");
}
}
(function update() {
getLocationUpdate();
})(); // automatically invoke for first run
</script>