2017-11-25 07:49
浏览 130


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);

    function geoError() {
        alert("Geocoder failed.");
    function geocoding(glat,glng){


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;
        //Get address from json data
        $location = $data->results[0]->formatted_address;
        //$location = $data->results[0]->address_components;

           if(in_array("locality", $cn))
            $city= $data->results[0]->address_components[$j]->long_name;

        echo 'No Location';

    echo $city;


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

    点赞 评论
  • duanqujing3863 2017-11-25 07:56

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

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

    点赞 评论
  • dpxpz37157 2017-11-25 08:06

    initially start session in PHP using


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

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

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

    $city = $_SESSION['yoursessioname'];

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

    点赞 评论
  • douti6740 2017-11-25 10:38

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

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

相关推荐 更多相似问题