2013-12-15 26 views
0

我有一個標準的HTML表單提交登錄數據到服務器,但我也想從javascript提交用戶地理位置。我使用另一個stackoverflow問題的地理位置代碼:發送登錄和地理位置(從JavaScript)到服務器

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

回答

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> 

這是一個例子登錄表單登載有數據轉換爲「/script.php」,並且還有兩個隱藏字段,它們將具有位置數據(由下面的JS設置)。

的Javascript:

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

這個功能應該更換您的原代碼handlesucces功能;它使用位置數據設置上面定義的兩個隱藏字段。

相關問題