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.

1
  • Thanks for the reply, used a different method though. Commented Apr 10, 2013 at 22:45

2 Answers 2

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
});
Sign up to request clarification or add additional context in comments.

2 Comments

addition to this statement, jQuery makes it really easy with their $.ajax method. api.jquery.com/jQuery.ajax
Thanks for the reply, used a different method though.
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
}
?>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.