in this code i need to assign lat and long as a php variable .. pls help me
<!DOCTYPE html><html><body><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.";}
}
function showPosition(position)
{
x.innerHTML="Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
}
getLocation()
</script>
This assumes that you have a 'users' table with long and lat columns.
somescript.php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myDB";
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$stmt = $conn->prepare("INSERT INTO users (long, lat) VALUES (:long, :lat)");
$stmt->bindParam(':long', $long);
$stmt->bindParam(':lat', $lat);
$long = $_REQUEST['long'];
$lat = $_REQUEST['lat'];
$stmt->execute();
Your javascript:
function getLocation()
{
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(showPosition);
}
}
function showPosition(position)
{
var lat = position.coords.latitude;
var long = position.coords.longitude;
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
alert("Success");
}
};
xmlhttp.open("GET", "somescript.php?lat=" + lat + "&long=" + long, true);
xmlhttp.send();
}
getLocation();
similarly you would do the same for an UPDATE statement.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments