hyster Posted October 3, 2011 Share Posted October 3, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/248348-javascript-var-to-php-var/ Share on other sites More sharing options...
sunfighter Posted October 4, 2011 Share Posted October 4, 2011 php runs on the SERVER and runs prior to the web page creation. javascript runs on the user side in the browser that displays the web page. php can pass variables to js, but js can't pass then to php UNLESS you use AJAX. Whole other thing to study. Quote Link to comment https://forums.phpfreaks.com/topic/248348-javascript-var-to-php-var/#findComment-1275764 Share on other sites More sharing options...
HanneSThEGreaT Posted October 7, 2011 Share Posted October 7, 2011 I was also wondering about this very same thing, thank you for clearing that up albeit unknowingly Quote Link to comment https://forums.phpfreaks.com/topic/248348-javascript-var-to-php-var/#findComment-1276739 Share on other sites More sharing options...
Andy-H Posted October 7, 2011 Share Posted October 7, 2011 document.getElementById('distance').innerHTML += response.routes[0].legs[0].distance.value + " meters"; :-\ Get with the times Quote Link to comment https://forums.phpfreaks.com/topic/248348-javascript-var-to-php-var/#findComment-1276744 Share on other sites More sharing options...
haku Posted October 11, 2011 Share Posted October 11, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/248348-javascript-var-to-php-var/#findComment-1278160 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.