Jump to content

Recommended Posts

i no nothing about JS.

 

im messing about with google api v3 and i found a script i was able to modify to some extent.

im stuck on the last peice of JS i want to edit before i can move into more php.

 

im assuming i need to add php to this peice of code but i dont no how.

ive worked out that this adds the data to a html element (which i can display ok) but need the data into php.

 

thanks for any help

 

 

         document.getElementById('distance').innerHTML +=  
            response.routes[0].legs[0].distance.value + " meters"; 

 

 

full script

<!DOCTYPE html> 
<html>  
<head>  
   <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>  
   <title>Google Maps JavaScript API v3 Example: Directions Complex</title>  
   <script type="text/javascript"  
           src="http://maps.google.com/maps/api/js?sensor=false"></script> 
</head>  
<body style="font-family: Arial; font-size: 13px; color: red;">  

<?php
$line1 = "B8 1RJ";
$line2 = "B14 7JS";

echo "<div id=\"duration\">Duration: </div> ";
echo "<div id=\"distance\">Distance: </div> ";
?>
   <script type="text/javascript">  

   var directionsService = new google.maps.DirectionsService(); 
   var directionsDisplay = new google.maps.DirectionsRenderer(); 
  

   var request = { 
       origin: '<?php echo $line1 ?>',  
       destination: '<?php echo $line2 ?>', 
       travelMode: google.maps.DirectionsTravelMode.DRIVING 
   }; 

   directionsService.route(request, function(response, status) { 
      if (status == google.maps.DirectionsStatus.OK) { 

         // Display the distance: 
         document.getElementById('distance').innerHTML +=  
            response.routes[0].legs[0].distance.value + " meters"; 

         // Display the duration: 
         document.getElementById('duration').innerHTML +=  
            response.routes[0].legs[0].duration.value + " seconds"; 


         directionsDisplay.setDirections(response); 
      } 
   }); 
   </script> 

</body>  
</html>

Link to comment
https://forums.phpfreaks.com/topic/248348-javascript-var-to-php-var/
Share on other sites

jQuery is awesome, and I use it all the time. But that said, it's not appropriate for everything, and if the OP only has a few lines of code, the using jQuery would be stupid as it would just add unnecessary overhead. You seem to have fallen into the trap of thinking that just because something is good some of the time, that means it's good all of the time.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.