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';
}
-
1Hi Vagelis, I've attempted to clean up the question - please let me know if the changes are not correctStephen Lead– Stephen Lead2014年11月04日 22:01:14 +00:00Commented Nov 4, 2014 at 22:01
-
Can you edit your question to summarize what you are asking in its title, please? How to frame a good question? is well worth a review.PolyGeo– PolyGeo ♦2014年11月04日 22:03:13 +00:00Commented Nov 4, 2014 at 22:03
-
Simplest way is to create php call to postgres and generate xml or json official way uses mysql developers.google.com/maps/articles/…Mapperz– Mapperz ♦2014年11月04日 22:16:22 +00:00Commented Nov 4, 2014 at 22:16
-
Mapperz i use Postgresql but not mysqlvagelis– vagelis2014年11月05日 19:58:19 +00:00Commented Nov 5, 2014 at 19:58
1 Answer 1
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.
Explore related questions
See similar questions with these tags.