[GIS] Google Maps Api , PHP, Javascript and Postgresq-Postgis

google-maps-apijavascriptPHPpostgispostgresql

I'm trying to make a web application, where I take the start/end city from a text box, geocode the city, use the coordinates to query my database in PHP, and return the features to be drawn in the Google Maps API.

I retrieve the latitude and longtitude using the script at https://developers.google.com/maps/documentation/javascript/examples/geocoding-simple?hl=EL

But I dont know how to pass the latitude and longtitude in PHP to make a select to database. (I plan to return the features to JavaScript, so I can visualize it with the script at https://developers.google.com/maps/documentation/javascript/examples/polyline-simple?hl=EL)

How can I pass the value from the HTML/JavaScript to the PHP file where I have the connection with database?

I try with jQueries but return me an empty vlaues the function i use is

function codeAddress1() {
  var address1 = document.getElementById('address1').value;
  geocoder.geocode( { 'address': address1}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      latitudeEnd = results[0].geometry.location.lat();
      longitudeEnd = results[0].geometry.location.lng();
    }
  });

$.ajax({
     data: {latitudeEnd : latitudeEnd, longitudeEnd: longitudeEnd},
     type: "POST",
     url: "script.php",
     success: function (e) {
          alert(e);
     },
     error: function (er) {
          alert("Error: " + er);
     }
 });
  document.location.href = 'script.php';
}

Best Answer

How can I pass the value from the HTML/JavaScript to the PHP file where I have the connection with database?

If you use jQuery, you can use an AJAX call like this:

$.ajax({
     data: {lat : lat, lng: lng},
     type: "POST",
     url: "script.php",
     success: function (e) {
          alert(e);
     },
     error: function (er) {
          alert("Error: " + er);
     }
 });

And your PHP:

<?php
    $lat = $_POST['lat'];
    $lng = $_POST['lng'];

    //Do what you want with the data, connect to databases, perform queries, etc.  You can then echo back the information from the queries and process the data

    echo 'Your latitude is: ', $lat, ' and your longitude is: ', $lng  //Only an example to show you that the lat/lng params are now in PHP
?>

I know it's a bit vague but this answers your question above regarding how to send data to PHP.

Also, here is a post that has a bit more in regards to posting the information to a PHP for use of querying a database.