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
If you use jQuery, you can use an AJAX call like this:
And your 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.