将登录和地理位置(从javascript)发送到服务器


Send Login and Geolocation (from javascript) to Server

我有一个标准的HTML表单,可以向服务器提交登录数据,但我也想从javascript提交用户的地理位置。我使用了另一个stackoverflow问题的地理定位代码:

window.onload = function(){
if(navigator.geolocation)
    navigator.geolocation.getCurrentPosition(handlesucces, onError);
}
 function handlesucces(location) {
location.coords.latitude;
location.coords.longitude;
 }
 function onError() {
document.write("Error");
}

但我不确定如何将这些数据从HTML表单添加到POST

HTML:

<form method=POST action="/script.php">
Username : <input type=text name=user>
Password : <input type=password name=pass>
<input type=hidden id=latitude name=latitude>
<input type=hidden id=longitude name=longitude>
<button type=submit>Submit</button>

这是一个示例登录表单,它将数据发布到"/script.php",并且还有两个隐藏字段,它们将具有位置数据(由下面的JS设置)。

Javascript:

function handlesucces(location) {
    document.getElementById("latitude").value = location.coords.latitude;
    document.getElementById("longitude").value = location.coords.longitude;
}

此函数应替换原始代码中的handlesucces函数;它用位置数据设置上面定义的表单的两个隐藏字段。