Here is a working example, you need to play with it to fit your needs:
First: HTML/JavaScript/jQuery
index.html
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js">
</script>
</head>
<body>
<script>
window.setInterval(function() {
showlocation();
}
, 2000); // 2000==2 seconds
function showlocation() {
navigator.geolocation.watchPosition(callback);
}
function callback(position) {
var lat = position.coords.latitude;
var lon = position.coords.longitude;
document.getElementById('latitude').innerHTML = lat;
document.getElementById('longitude').innerHTML = lon;
$.post('processor.php', 'lat=' + lat + '&lon=' + lon, function(response) {
//alert(response);
}
);;
}
</script>
Latitude: <span id="latitude"></span>
<br/>
Longitude: <span id="longitude"></span>
</body>
</html>
Second:
processor.php
<?php
$lat=$_POST["lat"];
$lon=$_POST["lon"];
// insert into database
$conn = mysqli_connect("localhost","user","pass","database");
$sql="INSERT INTO location_tbl (latitude, longitude) VALUES ('$lat','$lon')";
// You might need some conditional insert here and for every 2 seconds you will get lots of records in database!
mysqli_query($conn,$sql) or die(mysqli_error($conn));
// return 'done';
?>
This stack-overflow post could be a good reference
Note that it need user permission to share location, otherwise it won't work properly.
The solution provided is for educational purpose only...
There are many other practical ways to achieve the question's goal.
Why don't you use some PHP IP Geo Location Class/API, and re-log the Longitude, Latitude once changed?