IT.COM

Passing variables from Java Script to PHP

Spaceship Spaceship
Watch
Impact
13
Hi everyone! Can you tell me how to pass variables from Java Script to a PHP File?


This works with a .js file, but how can I make it work with php.

html + .js file

<body>
<h1>Example</h1>
<div id="geo" class="geolocation_data"></div>
<script type="text/JavaScript" src="geo.js"></script>
</body>
</html>



html + php file - How do I have to change the code above to make it work with
the php file below?

<?php

$geo = 'http://maps.google.com/maps/api/geocode/xml?latlng='.htmlentities(htmlspecialchars(strip_tags($_GET['latlng']))).'&sensor=true';
$xml = simplexml_load_file($geo);

...


Thanks for your help.
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
0
•••
you can just echo the code that returns the value of the variable.
 
0
•••
php to javascript is easy since php runs before and you can echo the javascript, if you can make it work on ajax (best solution but a little more complex) then you are on the other side

when I need JS to PHP I usually make a cookie and retreive the data 1 click late or by running a second include with the echo
 
0
•••
dude just use a hidden value in the JS
 
0
•••
<script type="text/javascript">
var myVar = "<?PHP echo $phpVar; ?>";
</script>
 
0
•••
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back