views:

70

answers:

3

Ok.

Basically I am utilizng the Google Maps API - It is Javascript.

My site runs off PHP for the most part.

My intention is to make calls to the Google Maps API to get the distance between various already geocoded longitude and latitude points. I then want to display them to my users.

An example.

I have (in php):

$home=array(latitude,longitude); 

I also have a mysql_query result from my database of locations.

As such I do

while($result=mysql_fetch_array($results)){

//OUTPUT THE GOOGLE MAPS JAVASCRIPT JARGON WHICH WILL FIND THE DISTANCE
//BETWEEN MY HOME AND THE DB LOCATIONS

}

In the example code the value is simply outputted to a div by setting the inner html of a specified div to the response value of a certain query (this is where my JS knowledge gets hazy).

Ideally I want to get this value into a php var.

Is there anyway of doing:

$var=response from js thing;

What I have done at the moment is essentially using php I have generated the correct number of divs with names 'response1','response2' etc which the JS populates.. but this does not seem like the most logical way of doing things.

Hope that makes sense. Thanks

A: 

I would recommend taking a look at this part of the Google Maps API.

Valera
Thanks - but seemingly irrelevant. Ive coded the script with the API, and it works. My question is about passing JS results to PHP.
Thomas Clowes
In that case, you could write some AJAX code to communicate the results back to the server, or redirect to another (possibly the same) page with some parameters in the URL.
Valera
Perhaps you're trying to do something like: Start PHP processing, process JS, return result(s) from JS to PHP, finish PHP processing, render the page. That will only be possible like: Do PHP processing, send page to user, process JS, use AJAX to return result to server, change page to reflect result of AJAX call.<br /><br />It would not be possible the first way...
Valera
A: 

Yes! I did this just the other day:

in your PHP file do this (taken straight from my code):

<?php
$numbers = array(5, 2, 10, 15);
/* Model and stuff here */
?>
<html>
    <head>
        <script type="text/javascript">
// We're going to put all the info we need here, as a JS global variable.
// If you need even more control, simply fetch it via JQuery from a PHP script that does the following.
var numbers = <?php echo json_encode($numbers); ?>

alert(numbers);
// Expected output: [5,2,10,15]
alert(numbers[2]);
// Expected output: 10
        </script>
    </head>
</html>

Cheers!

hopeseekr
I think ive missed the point here.I am working directly with the Google Maps API. No other frameworks etc.So I make a call to the API and it returns various values.Im trying to get this value returned by JS into a php var. I.E the other way around i believe.Thanks
Thomas Clowes
+1  A: 

To send an answer from JavaScript to PHP requires AJAX:

If you can use JQuery, which is pretty much a standard, it's really easy. This is verbatim from one of the lessons I wrote for my PHPPro course:

receiveNumbers.php

<?php
if (isset($_GET['sentNums']))
{
    $sentNumbers = filter_input(INPUT_GET, 'sentNums', FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
    // Convert to array.
    $numbers = split(', ', $sentNumbers);
    // Echo out.
    echo json_encode($numbers).
}

index.php

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
    <head>
        <title>Graph Sort | PHPExperts.pro</title>
        <script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
        <script type="text/javascript" src="js/jquery.json-2.2.min.js"></script>
        <script type="text/javascript">
var myNumbers = '5, 10, 22, 11';
var receivedNumbers;

<?php
// Send data from JavaScript -> PHP.
if (!isset($_GET['myData']))
{
?>
    jQuery.getJSON('receiveNumbers.php?sentNums=' + myNumbers, function(jsonReceived)
    {
        receivedNumbers = jQuery.parseJSON(jsonReceived);
        alert(receivedNumbers); // Expected [5,10,22,11]
    });
<?php
}
?>
       </script>
   </head>
</html>
hopeseekr