How to pass the value of ajax variable return php

I know that this question is asked many times, but they do not have the right solution. I am using ajax to get a response from a PHP Page. Having received the answer, I want to use the value in a PHP variable. Below the code gets the result, but I am confused with its use.

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 $cityI get the name of the city, but in the control elements it is shown as

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

the problem is that I cannot use this in MYSQL because it is in html format and I just want to get only the name of the city, nothing more.

I hope my problem will be clear, please leave a comment if not clear.

+4
source share
4 answers
  • example.com/index.php, -.
  • JS.
  • .
  • index.php

? , . . Webservers -. php , , ei. $city . . - , , index.php . , , , index.php, .

, , SQL- geolocation.php. Google, . $city HTML-, . , , .

0

, . ()?

edit: json php eval(), ?

0

PHP strip_tags, HTML , City , :

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

PHP

sessionstart()

. . Jquery, :

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

PHP, :

$city = $_SESSION['yoursessioname'];

I have not tried it yet. I hope this helps. :)

-1
source

Source: https://habr.com/ru/post/1689810/


All Articles