0

Hey guys I want the longitude and latitude I get from the javascript code to pass it to the php variables $lat and $lng so I can get the $city and use it in my sql query(query not included). Below is my code. Please help

        <html>

<head>
<script>
function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition);
    } else {
        document.write("Geolocation is not supported by this browser.");
    }
}
function showPosition(position) {

  var latitude= position.coords.latitude;
  var longitude= position.coords.longitude); 

}
</script>
</head>
<body onload="getLocation()"></body>
</html>
<?php

 function getaddress($lat,$lng)
  {
     $url = 'http://maps.googleapis.com/maps/api/geocode/json?latlng='.trim($lat).','.trim($lng).'&sensor=false';
     $json = @file_get_contents($url);
     $data=json_decode($json);
     $status = $data->status;
     if($status=="OK")
     {
       return $data->results[0]->formatted_address;
     }
     else
     {
       return false;
     }
  }
   $lat= 33.568711799999996; //latitude
  $lng= 35.3800354; //longitude
  $address= getaddress($lat,$lng);
  if($address)
  {
    // Delimiters may be slash, or comma
    //$date = "04/30/1973";
    list($address, $street, $city) = split('[,-]', $address);
    echo "Address: $address; Street: $street; City: $city <br />\n";

  }
  else
  {
    echo "Not found";
  }
?>
3
  • What result does your code produce, what result do you expect it to produce? Commented May 4, 2018 at 7:59
  • Are the HTML and the PHP in different files? You would need to use Ajax to send the JS data to PHP. Commented May 4, 2018 at 8:01
  • They are in the same file. I want var longitude and latitude to be passed to $lng and $lat Commented May 4, 2018 at 8:03

1 Answer 1

1

You should move your PHP script to another file for e.g. script.php than with jquery get

$.get( "script.php", { lat: "123", lon: "321" }, function( data ) {
    alert( data ); // or you can insert result text to your block
});

And start your PHP script with this:

if(isset($_GET['lat']) && isset($_GET['lon']))

P.S. Don't forget to include jquery lib

Sign up to request clarification or add additional context in comments.

9 Comments

This answer does solve the issue. However, you can make the Ajax call in vanilla JS if jquery isn't used anywhere else in your app. Including a full and bloated js-lib just for one Ajax-call is overkill.
@MagnusEriksson There are many solutions of this problem, thats the way I solve it.
My point was that recommending using jQuery (if it isn't already in use) just to make one single ajax call is silly. You should always try and keep your application as light as possible and not just include bloated libraries when there's no need to.
@MagnusEriksson You can add ajax only - projects.jga.me/jquery-builder if you have another solution without using additional libraries you can post it here.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.