dth20986 2017-11-25 07:49
浏览 134
已采纳

如何传递ajax返回值php变量

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.

below is my 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

<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.

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

  • 写回答

4条回答 默认 最新

  • douzhi1937 2017-11-25 15:30
    关注
    • The user locates example.com/index.php, it displays a webpage.
    • You get the user's location from 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

    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.

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 使用R语言GD包一直不出结果
  • ¥15 计算机微处理器与接口技术相关问题,求解答图片的这个问题,有多少个端口,端口地址和解答问题的方法和思路,不要AI作答
  • ¥15 如何根据一个截图编写对应的HTML代码
  • ¥15 stm32标准库的PID角度环
  • ¥15 ADS已经下载好了,但是DAS下载不了,一直显示这两种情况,有什么办法吗,非常急!
  • ¥100 Excel 点击发送自动跳转outlook邮件
  • ¥15 gis中用栅格计算器或加权总和后图层不显示,值也明显不对
  • ¥15 python使用python-pptx如何给幻灯片添加只读密码。
  • ¥15 深度神经网络传递自变量损失
  • ¥15 oracle数据库备份表如何操作