0

I'm trying to use the Geocoding service in the Google Maps API to get the latitude and longitude values for an address, and then pass the result to PHP to connect to a database and find locations within a certain distance from the found address. It will then pass the results back to javascript to display the results on a google map.

Everything is working, however I can't seem to pass the variable from the javascript to php. I understand this is because php is server side and javascript is client side however I've seen a couple of forums that explain how it can be done, but not on the same file.

So is there a way to pass javascript variables to php in the same file?

Many thanks.

DJDMorrison
  • 1,302
  • 2
  • 17
  • 34

2 Answers2

1

By the sounds of it you need to use AJAX. This will allow you to send the JavaScript variable to a PHP page to do the database lookup and return a value.

Using jQuery for example:

//All your Google maps code here ...
$.get('databaselookup.php',{lat: latitude,lng: longitude}, function(data) {
    //Do something in here with the returned value from the PHP file
});
OdinX
  • 4,135
  • 1
  • 24
  • 33
0

You can use GET Variable.

Eg:

<?php 
if(isset($_GET["Result"])) 
{
    /*DO WORK WITH THE VARIABLE*/

}
else
{
?>
 <SCRIPT language="JavaScript"> 
 var JSVARIABLE="Foo";
 location.href="pageurl.php?Result=" + JSVARIABLE; 
 </SCRIPT>
<?php
}
?>
Ritesh Kumar Gupta
  • 5,055
  • 7
  • 45
  • 71