gpt4 book ai didi

php - 从 JavaScript 获取位置并将其传递给 PHP

转载 作者:行者123 更新时间:2023-11-29 17:17:00 26 4
gpt4 key购买 nike

我想使用 javascript 获取经度和纬度,而不是将其与之前的位置进行比较。当您从另一台计算机登录时,Facebook 会做一些类似的事情,但要简化得多。为了实现这一点,我创建了 2 个隐藏字段,当用户提交表单时,值将被发送到服务器。

<body>
<p id="log">Login to continue:
<form action="welcome.php" method="post">
Username: <input type="text" name="user" required >
Password: <input type="password" required>
<input type="hidden" name= "longitude" id="longitude">
<input type= "hidden" name ="latitude" id="latitude">

<input type="submit" name="submit" onclick="getLocation()">
</p>
<script>
var x=document.getElementById("log");
function getLocation()
{
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(showPosition);
}
else{x.innerHTML="Geolocation is not supported by this browser.";}
}
function showPosition(position)
{
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
document.getElementByID("longitude").value = longitude;
document.getElementByID("latitude").value = latitude;

}

</script>

但是在 welcome.php 中的代码:

$latitude=$_POST["latitude"];
$longtude=$_POST["longitude"];

返回: 注意:C:\xampp\htdocs\welcome.php 第 8 行未定义索引:latitude

注意:C:\xampp\htdocs\welcome.php 第 9 行未定义索引:longitude

是否有更好的方法来做到这一点,以及如何解决这个问题?

最佳答案

您正尝试通过 ID 访问元素,但它们没有 ID。

为它们添加一个 ID 属性,或者以不同的方式引用它们。

关于php - 从 JavaScript 获取位置并将其传递给 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16762180/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com