Shivansh Chaudhri
Shivansh Chaudhri

Reputation: 7

How to store location of visitor's profile on mysql database on php using javascript?

<!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

Answers (3)

Nihal
Nihal

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

Priya
Priya

Reputation: 1470

Use ajax of jquery to store data when you get position info

Upvotes: 0

Elbek
Elbek

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

Related Questions