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

时间:2013-12-15 12:39:17

标签: javascript php

我有一个标准的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

1 个答案:

答案 0 :(得分:3)

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>

这是一个示例登录表单,它将数据POST到“/script.php”,并且还有两个隐藏字段,这些字段将包含位置数据(由下面的JS设置)。

Javascript:

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

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

相关问题