我在这里尝试做的是将 JavaScript 变量发送到 PHP 函数。我正在开发的是一个 jQuery 移动应用程序,当您到达特定的经度/纬度时,您可以在其中签到。但是我在将经度/纬度发送到 PHP 函数时遇到了问题。我将它发送到 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,但没有成功。任何帮助将不胜感激。
最佳答案
您在收到地理定位响应之前发送请求
您需要在回调中执行此操作。
关于php - 将 JavaScript 变量发送到 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8342486/