【发布时间】:2019-04-25 16:09:54
【问题描述】:
我想调用一个页面来获取地理位置,然后自动将表单提交到我的 php 页面到 $_REQUEST getlat 和 getlon 而无需单击提交按钮。这就是我所拥有的。在我的浏览器检查器中,我看到了我的地理位置值,但它不会自动提交。我尝试在我的 body 标签中使用 onload 函数,但后来发现你一次只能调用一个函数,而且我读到这是一个不好的做法。我已经查看了另一个类似的问题,但无法将其拼凑在一起。感谢您的帮助
<html>
<script>
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
showPosition,
positionError
);
}
}
function showPosition(position) {
document.getElementById('getlat').value = position.coords.latitude;
document.getElementById('getlon').value = position.coords.longitude;
lon = document.getElementById('getlon').value;
lat = document.getElementById('getlat').value;
}
function positionError(error) {
if (error.PERMISSION_DENIED) alert('Please accept geolocation');
hideLoadingDiv();
showError(
'Geolocation is not enabled. Please enable to use this feature'
);
}
</script>
<script>
function PageLoad() {
getLocation();
document.frm1.submit();
}
</script>
<body>
<script>
window.onload = PageLoad();
</script>
<form action="results.php" name="frm1">
<input type="hidden" name="longitude" id="getlon" />
<input type="hidden" name="latitude" id="getlat" />
</form>
</body>
</html>
【问题讨论】:
标签: javascript php html geolocation