发送JavaScript变量到PHP [英] Send JavaScript variable to PHP

查看:82
本文介绍了发送JavaScript变量到PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么即时试图做的,是发送一个JavaScript变量到PHP函数。我正在开发一个jQuery Mobile的应用程序,你可以检查,当你达到一个特定的经度/纬度。但我有与发送的经度/纬度到PHP函数的问题。林把它发送到一个PHP函数,因为我也会做的,我已经保存在我的数据库中的经度/纬度的对比。

What im trying to do here is sending a JavaScript variable to a PHP function. What I'm developing is a jQuery Mobile App where you can check in when you reach a specific longitude/latitude. But I'm having problem with sending the longitude/latitude to the PHP-function. Im sending it to a PHP-function because im gonna do a comparison with the longitude/latitude that I've stored in my database.

下面是我的code。

        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,但我没有得到它的工作。任何帮助将AP preciated。

How do I approach this problem? I've tried with JSON, but i didn't get it to work. Any help will be appreciated.

推荐答案

您要发送请求之前,你的地理位置回应

You're sending the request before you get the geolocation response

您需要做的是在回调中。

You need to do it inside the callback.

这篇关于发送JavaScript变量到PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆