如何传递ajax返回值php变量

时间:2022-06-18 00:02:17

i know this question is asked many times, but non of them having right solution. i am using ajax to get the response from PHP Page. After getting the response i want to use the value in PHP variable. Below code is getting result but i am confused with the usage of it.

我知道很多次都会问这个问题,但他们没有正确的解决方案。我正在使用ajax从PHP页面获取响应。获得响应后,我想使用PHP变量中的值。下面的代码得到的结果,但我对它的用法感到困惑。

below is my index.php

下面是我的index.php

function getLocation() {
     if(navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(geoSuccess, geoError);
        } else {
            alert("Geolocation is not supported by this browser.");
        }
    }

    function geoSuccess(position) {
        var glat = position.coords.latitude;
        var glng = position.coords.longitude;
        //alert("lat:" + glat + " lng:" + glng);
        geocoding(glat,glng);
      }

    function geoError() {
        alert("Geocoder failed.");
    }
    function geocoding(glat,glng){
        $.ajax({
        type:'POST',
        url:'geolocation.php',
        data:'latitude='+glat+'&longitude='+glng,
        success:function(result){
            if(result){
               $("#locationg").val(result);
               $("#htmllocation").html(result);
           }
        }
    });
   }

geolocation.php

<?php
session_start();
if(!empty($_POST['latitude']) && !empty($_POST['longitude'])){
    //Send request and receive json data by latitude and longitude
    $url = 'http://maps.googleapis.com/maps/api/geocode/json?latlng='.trim($_POST['latitude']).','.trim($_POST['longitude']).'&sensor=false';
    $json = @file_get_contents($url);
    $data = json_decode($json);
    $status = $data->status;
    if($status=="OK"){
        //Get address from json data
        $location = $data->results[0]->formatted_address;
        //$location = $data->results[0]->address_components;

        for($j=0;$j<count($data->results[0]->address_components);$j++){
               $cn=array($data->results[0]->address_components[$j]->types[0]);
           if(in_array("locality", $cn))
           {
            $city= $data->results[0]->address_components[$j]->long_name;
           }
            }

     }else{
        echo 'No Location';
    }

    echo $city;
}
?>

index.php

<?php
    $city='<span id="htmllocation"></span>';
?>

when i echo $city i am getting city name but in inspect elements its showing like

当我回应$ city时,我正在获得城市名称,但在检查元素中它的显示就像

<span id="htmllocation">Visakhapatnam</span>

issue is that i can not use this in MYSQL because it in html format, and i just want to get only the city name nothing else.

问题是我不能在MYSQL中使用它,因为它采用html格式,而我只想获得城市名称。

i hope my issue is clear, please leave a comment if not clear.

我希望我的问题很清楚,如果不清楚请发表评论。

4 个解决方案

#1


0  

  • The user locates example.com/index.php, it displays a webpage.
  • 用户找到example.com/index.php,它显示一个网页。

  • You get the user's location from JS.
  • 您从JS获得用户的位置。

  • You send it back to the server, and get the addresses of that location.
  • 您将其发送回服务器,并获取该位置的地址。

  • Then you want to access the addresses from index.php
  • 然后你想从index.php访问地址

Is that correct? If so, you can't do it. Things not work like that. Webservers uses request-response modells. When your php finishes, the server kills the process, ei. $city and everything else are destroied. After it, you get the user's location. If you want something from the server again, you must send a new request, because index.php's process is no longer available. This is impossible to get the city's name before you get it from the client, and you can't get the location from the client at the moment he first requests index.php, neither you can access an already-non-running process.

那是对的吗?如果是这样,你就做不到。事情不像那样。 Web服务器使用请求 - 响应模式。当你的php完成时,服务器会终止进程,ei。 $ city和其他一切都被破坏了。之后,您将获得用户的位置。如果您想再次从服务器获取某些内容,则必须发送新请求,因为index.php的进程不再可用。在从客户端获取城市名称之前,这是不可能获得该城市的名称,并且在他第一次请求index.php时您无法从客户端获取该位置,您也无法访问已经未运行的进程。

All you need to do is run your SQL query inside geolocation.php. When you get the result from Google, then fetch the data from your database. There $city doesn't contain any HTML codes, only the plain city name. Then send back some data related to that city to the user, and display it.

您需要做的就是在geolocation.php中运行SQL查询。从Google获得结果后,从数据库中获取数据。 $ city不包含任何HTML代码,只包含普通城市名称。然后将与该城市相关的一些数据发回给用户,并显示它。

#2


0  

I don't have enough reput to comment then I ask here. May you post a dump of geocoding() call ?

我没有足够的评论来评论然后我在这里问。你可以发一个geocoding()调用转储吗?

edit: try to json encode your php response and use eval(), it's quick and dirty?

编辑:尝试json编码你的PHP响应并使用eval(),它的快速和脏?

#3


0  

Use PHP in built function strip_tags to remove HTML tags from your statement to get only City in variable like below:

在内置函数strip_tags中使用PHP从语句中删除HTML标记,以便仅在变量中获取City,如下所示:

$city = strip_tags('<span id="htmllocation">Visakhapatnam</span>');
// Output will be Visakhapatnam only

#4


-1  

initially start session in PHP using

最初使用PHP启动会话

sessionstart()

method. then add this code after above code. First set session using Jquery as below:

方法。然后在上面的代码之后添加此代码。首先使用Jquery设置会话,如下所示:

$.session.set("yoursessioname", "storevalue");

then try to get this session variable in PHP as below:

然后尝试在PHP中获取此会话变量,如下所示:

$city = $_SESSION['yoursessioname'];

I haven't tried it yet. I Hope it helps. :)

我还没有尝试过。我希望它有所帮助。 :)

#1


0  

  • The user locates example.com/index.php, it displays a webpage.
  • 用户找到example.com/index.php,它显示一个网页。

  • You get the user's location from JS.
  • 您从JS获得用户的位置。

  • You send it back to the server, and get the addresses of that location.
  • 您将其发送回服务器,并获取该位置的地址。

  • Then you want to access the addresses from index.php
  • 然后你想从index.php访问地址

Is that correct? If so, you can't do it. Things not work like that. Webservers uses request-response modells. When your php finishes, the server kills the process, ei. $city and everything else are destroied. After it, you get the user's location. If you want something from the server again, you must send a new request, because index.php's process is no longer available. This is impossible to get the city's name before you get it from the client, and you can't get the location from the client at the moment he first requests index.php, neither you can access an already-non-running process.

那是对的吗?如果是这样,你就做不到。事情不像那样。 Web服务器使用请求 - 响应模式。当你的php完成时,服务器会终止进程,ei。 $ city和其他一切都被破坏了。之后,您将获得用户的位置。如果您想再次从服务器获取某些内容,则必须发送新请求,因为index.php的进程不再可用。在从客户端获取城市名称之前,这是不可能获得该城市的名称,并且在他第一次请求index.php时您无法从客户端获取该位置,您也无法访问已经未运行的进程。

All you need to do is run your SQL query inside geolocation.php. When you get the result from Google, then fetch the data from your database. There $city doesn't contain any HTML codes, only the plain city name. Then send back some data related to that city to the user, and display it.

您需要做的就是在geolocation.php中运行SQL查询。从Google获得结果后,从数据库中获取数据。 $ city不包含任何HTML代码,只包含普通城市名称。然后将与该城市相关的一些数据发回给用户,并显示它。

#2


0  

I don't have enough reput to comment then I ask here. May you post a dump of geocoding() call ?

我没有足够的评论来评论然后我在这里问。你可以发一个geocoding()调用转储吗?

edit: try to json encode your php response and use eval(), it's quick and dirty?

编辑:尝试json编码你的PHP响应并使用eval(),它的快速和脏?

#3


0  

Use PHP in built function strip_tags to remove HTML tags from your statement to get only City in variable like below:

在内置函数strip_tags中使用PHP从语句中删除HTML标记,以便仅在变量中获取City,如下所示:

$city = strip_tags('<span id="htmllocation">Visakhapatnam</span>');
// Output will be Visakhapatnam only

#4


-1  

initially start session in PHP using

最初使用PHP启动会话

sessionstart()

method. then add this code after above code. First set session using Jquery as below:

方法。然后在上面的代码之后添加此代码。首先使用Jquery设置会话,如下所示:

$.session.set("yoursessioname", "storevalue");

then try to get this session variable in PHP as below:

然后尝试在PHP中获取此会话变量,如下所示:

$city = $_SESSION['yoursessioname'];

I haven't tried it yet. I Hope it helps. :)

我还没有尝试过。我希望它有所帮助。 :)