2012-01-25 48 views
1

我已經瀏覽了幾個類似的線程 - 但無法確切地知道我要出錯的地方。 我正在使用AJAX & PHP創建一個應用程序的用戶lng & PHP。我知道AJAX & PHP的工作原理是將所有內容(甚至是虛擬的數據)保存到我的數據庫表中。 我一直在玩幾個小時的變量,迄今爲止我得到的最好結果是在數據庫中插入了一個「0」值。使用AJAX將地理位置數據發佈到PHP

document.addEventListener("deviceready", onDeviceReady, false); 
function onDeviceReady() { 
getCurrentLocation(); 
} 
function onError(message) { 
navigator.notification.alert(message, "", "Error"); 
} 
function getCurrentLocation() { 
navigator.geolocation.getCurrentPosition(locationSuccess, onError); 
} 
function locationSuccess(position) { 
lat = document.getElementById("latSpan"); 
lon = document.getElementById("latSpan"); 
latitude = position.coords.latitude; 
longitude = position.coords.longitude; 
} 
//recording function 
function getXMLObject() //XML OBJECT 
{ 
var xmlHttp = false; 
try { 
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP") // For Old Microsoft Browsers 
} 
catch (e) { 
try { 
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP") // For Microsoft IE 6.0+ 
} 
catch (e2) { 
xmlHttp = false // No Browser accepts the XMLHTTP Object then false 
} 
} 
if (!xmlHttp && typeof XMLHttpRequest != 'undefined') 
{ 
xmlHttp = new XMLHttpRequest();  //For Mozilla, Opera Browsers 
} 
return xmlHttp; // Mandatory Statement returning the ajax object created 
} 
var xmlhttp = new getXMLObject(); //xmlhttp holds the ajax object 
function ajaxFunction() { 
var getdate = new Date(); //Used to prevent caching during ajax call 
if(xmlhttp) { 
xmlhttp.open("POST","http://www.lauracrane.co.uk/app/rec/location.php",true); // 
xmlhttp.onreadystatechange = handleServerResponse; 
xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 
xmlhttp.send("latitude=" + latitude + "&longitude=" + longitude); 
} 
} 
function handleServerResponse() { 
if (xmlhttp.readyState == 4) { 
if(xmlhttp.status == 200) { 
    document.getElementById("message").innerHTML=xmlhttp.responseText; 
} 
else { 
alert("Error during AJAX call. Please try again"); 
} 
}}' 

我想知道是否有人能看到爲什麼它不緯度& LNG的值傳遞給AJAX功能。

任何幫助,將不勝感激。 :)

勞拉

+0

你檢查是否在PHP端接收的數據?因爲那應該看起來不錯。如果你使用INT來保存經度和緯度,那麼問題可能出現在數據庫端 – cleanunicorn

+0

嗨,它是VARCHAR--只是檢查一下,但感謝你的幫助。 :) – LCrane86

回答

0

也許我們不能看到所有的代碼,但它看起來像緯度和經度功能作用域locationSuccess。所以當你嘗試在ajaxFunction中訪問它們時,它們會得到默認值。

此外,你不需要做所有的getXMLObject樂趣。在WebKit瀏覽器像Android,iOS和黑莓手機,你只需要做:

var xmlhttp = new XMLHttpRequest(); 

最後,因爲你很可能運行這一關的文件://協議,你將不得不尋找0的狀態碼在這種情況下類似於200。

function handleServerResponse() { 
    if (xmlhttp.readyState == 4) { 
     if(xmlhttp.status == 200 || xmlhttp.status == 0) { 
      document.getElementById("message").innerHTML=xmlhttp.responseText; 
     } 
    } 
    // etc. 
} 
0

爲什麼不只是使用jQuery ajax調用?你的代碼被標記爲IE6 ...因爲這在phonegap標記,我會假設你有權使用新的方法做事。

我會建議使用jQuery AJAX ..

function ajaxFunction() { 
$.ajax({ 
    url:'http://www.lauracrane.co.uk/app/rec/location.php', 
    type:'POST', 
    data:'lat='+lat+'&long='+long, 
    success:function(d){ 
    console.log(d); 
    }, 
    error(w,t,f){ 
    console.log(w+' '+t+' '+f); 
    } 
}); 
}