2011-12-01 96 views
1

即時嘗試在這裏做的是發送一個JavaScript變量到PHP函數。我正在開發的是一個jQuery Mobile應用程序,您可以在達到特定的經度/緯度時進行檢查。但是我在向PHP函數發送經度/緯度時遇到了問題。我發送它到一個PHP函數,因爲我要做一個比較,我已經存儲在我的數據庫中的經度/緯度。發送JavaScript變量到PHP

這是我的代碼。

 function testResults() { 

      if(navigator.geolocation) { 
       //Get the current position 
       navigator.geolocation.getCurrentPosition(function(position) { 
        var latitude = position.coords.latitude; 
        var longitude = position.coords.longitude; 
        document.write("lat", latitude); 
        document.write('<br /><br />'); 
        document.write("long", longitude); 
       }); 
      } else { 
       alert("Sorry... your browser does not support the HTML5 GeoLocation API"); 
      } 

     //////////// Here I tried with jQuery/Ajax. I know that it's wrong. 
     //////////// Im putting it here so that it maybe clarifies what Im 
     //////////// trying to do. 

      $.post("checkLocation.php", { 
       lat : latitude, 
       longi : longitude 
      }, function(data) { 
       alert("Data Loaded: " + data); 
      }); 

     //////////////////////////////////////////////////////////////////// 

     } 

     testResults(); 

我該如何解決這個問題?我用JSON嘗試過,但我沒有得到它的工作。任何幫助將不勝感激。

+0

顯示你的PHP代碼 – Distdev

+0

請發表您的應用程序的PHP端(使用參數的檢索部分)> 並作出簡潔(wellformed)JSON: {「LAT」:緯度,「LNG」:經度} –

+0

@frank_neff:這是一個對象字面值,而不是JSON。沒有必要在那裏報價。 AJAX庫將對象序列化爲格式良好的JSON,完全獨立於源代碼是否帶引號。 – RoToRa

回答

2

你的地理位置響應

你需要做的是在回調中之前你發送請求。

+0

林不知道我是否明白你的意思? – Oscar

0

嘗試使用AJAX

http://api.jquery.com/jQuery.ajax/

你的處理程序應該是這樣的:

$.ajax({ 
    type: "POST", 
    url: "checklocation.php", 
    data: "lat="+latitude+"&longi="+longitude 
}).done(function(msg) { 
    alert("Data Saved: " + msg); 
}); 
+0

非常感謝。它看起來非常有幫助。我會馬上查找它。 – Oscar

+1

您也可以認真構建一個JSON對象,而不是僅僅構建一個發送數據的Javascript字符串: 'data:{lat:latitude,longi:longitude}' – Mattygabe

0
navigator.geolocation.getCurrentPosition(function(position) { 
       var latitude = position.coords.latitude; 
       var longitude = position.coords.longitude; 
       document.write("lat", latitude); 
       document.write('<br /><br />'); 
       document.write("long", longitude); 
      }); 

你有當地瓦爾這裏經度和緯度。如果你想使用他們這個功能之外,您可以使用全局變量:

var latitude; 
var longitude; 
//... 
navigator.geolocation.getCurrentPosition(function(position) { 
       latitude = position.coords.latitude; 
       vlongitude = position.coords.longitude; 
       document.write("lat", latitude); 
       document.write('<br /><br />'); 
       document.write("long", longitude); 
      }); 

//usage(transfer) of vars 
+0

由於某種原因,這並不奏效。奇怪:S – Oscar

0

嘗試使用回調AJAX請求(概念驗證,如果一切正常,沒有舒爾):

function testResults() 
{ 
    if (navigator.geolocation) 
    { 
     //Get the current position 
     navigator.geolocation.getCurrentPosition(function (position) 
     { 
      var latitude = position.coords.latitude; 
      var longitude = position.coords.longitude; 

      // do callback     
      $.ajax(
      { 
       type: "POST", 
       url: "checklocation.php", 
       data: "lat=" + latitude + "&lon=" + longitude 
      }).done(function (msg) 
      { 
       alert("Data Saved: " + msg); 
      }); 

     }); 
    } 
    else 
    { 
     alert("Sorry... your browser does not support the HTML5 GeoLocation API"); 
    } 
} 

testResults(); 

乾杯。弗蘭克

+0

參數(msg)有什麼用途? – Oscar

+0

我已經看到jqXHR.done()在1.8中已被棄用。改爲使用'success:function(){}'和'error:(){}'。 http://api.jquery.com/jQuery.ajax/ –