Reputation: 7
<!DOCTYPE html>
<html>
<body>
<p>Click the button to get your coordinates.</p>
<button onclick="getLocation()">Try It</button>
<p id="demo"></p>
<script>
var x = document.getElementById("demo");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
</script>
</body>
</html>
I want to store this in phpMyAdmin Database. without showing the user that the location is stored
Upvotes: 0
Views: 668
Reputation: 5334
Try this code in pc. dont run it on snippet because it needs you permission of location.
HTML:
<p>Click the button to get your coordinates.</p>
<button onclick="getLocation()">Try It</button>
<p id="demo1"></p>
<p id="demo2"></p>
<p id="demo3"></p>
<script>
var x = document.getElementById("demo1");
var y = document.getElementById("demo2");
var z = document.getElementById("demo3");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition1);
navigator.geolocation.getCurrentPosition(showPosition2);
navigator.geolocation.getCurrentPosition(showPosition3);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition1(position) {
alert("showPosition1");
x.innerHTML = "Latitude: " + position.coords.latitude + "<br>Longitude: " + position.coords.longitude;
}
function showPosition2(position) {
alert("showPosition2");
y.innerHTML = "Latitude: " + position.coords.latitude + "<br>Longitude: " + position.coords.longitude;
}
function showPosition3(position) {
alert("showPosition3");
z.innerHTML = "Latitude: " + position.coords.latitude + "<br>Longitude: " + position.coords.longitude;
}
</script>
</body>
</html>
Upvotes: 0
Reputation: 646
You just need to send the location to the certain php file (like geo.php
) using AJAX or any other request. And in the geo.php
you accept the data and write it to the database. Look for Ajax Post Request
Upvotes: 1