php - 将 JavaScript 变量发送到 PHP

标签 php javascript ajax json

我在这里尝试做的是将 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/

相关文章:

php - PayPal IPN 沙箱返回 HTTP 302

PHP - 将日期插入mysql

php - 如何在 PHP 中读取 excel 数据(office 2007 xlsx)?

php - 使用 PHP 和 Pushwoosh 开发的推送通知服务功能的适当系统流程应该是什么?

javascript - 无缝页面加载

javascript - Angular 模块不适用于 Gulp Babel

javascript - Laravel 自动页面重定向

php - JQuery 和 AJAX 发布到 php,无需在 WordPress 中重新加载页面

javascript - 如何使用 AJAX 对选择选项执行 PHP 查询?

javascript - 使用JavaScript自动提交表单