当用户单击按钮然后重定向到将值传递到PHP变量的新页面时,是否可以使用HTML5地理位置获取用户的纬度/经度?
因此,如果用户使用index.php
,其中包含HTML5地理位置代码,则他们单击此按钮;
<span class="button get-my-location">My Location</span>
然后将它们重定向到weather.php
将经纬度存储在2个PHP变量中的位置$lat
,$long
这可能吗?我认为它将需要基于AJAX?不幸的是,AJAX不是我的强项。
任何帮助都会很棒
获取用户的经度和纬度很容易,您将需要将其分配给表单中的隐藏字段,并将其提交到php页面网址,或者通过Javascript以GET或POST的形式发送
这是在拥有用户位置后如何直接重定向用户的示例:
<!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(redirectToPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function redirectToPosition(position) {
window.location='weather.php.php?lat='+position.coords.latitude+'&long='+position.coords.longitude;
}
</script>
</body>
</html>
PHP代码:
<?php
$lat=(isset($_GET['lat']))?$_GET['lat']:'';
$long=(isset($_GET['long']))?$_GET['long']:'';
//do whatever you want
?>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句