如何将以下代码中的经度和纬度传递给我的PHP代码?
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
}
}
function showPosition(position) {
document.getElementById("getlat").value = position.coords.latitude;
document.getElementById("getlon").value = position.coords.longitude;
}
HTML
<input type="hidden" id="getlat" name="getlat" value="<?php echo $_SESSION['polat']; ?>" />
<input type="hidden" id="getlon" name="getlon" value="<?php echo $_SESSION['polon']; ?>" />
PHP代码
$lat = '-27.486204'; // Should get latitude from JS here.
$long = '152.994962'; // Should get longitude from JS here.
mysqli_query("SELECT * From `table_nme` where `latitude`='.$lat.' AND `longitude`='.$long.'");
我知道如何在Query String Process中执行此操作,但我需要它而不使用Query String Process。
答案 0 :(得分:0)
使用以下Javascript代码:
var lat = position.coords.latitude,
long = position.coords.laongitude,
xhr = new XMLHttpRequest();
xhr.open('GET', 'http://url-to-server.xxx/page.php?polat=' + lat + '&polong=' + long, false);
xhr.send();
var result = xhr.responseText;