Passing PHP variables into Javascript
Whether you're a seasoned veteran or a struggling beginner, Web Radiance is the web development and web design forum for you. You'll find answers to all your HTML, CSS, SEO, and Programming needs. Pull up a chair and stay awhile.
Passing PHP variables into Javascript
#1
Posted 20 February 2008 - 01:28 PM
I have some longitude and latitude data in my database and want to use the google maps api to display them.
#2
Posted 20 February 2008 - 01:32 PM
<html> <?php $MyVar3 = "Something in PHP"; ?> <script language="JavaScript"> document.write(<?php print "'".$MyVar3."'"; ?>); </script> </html>
#3
Posted 20 February 2008 - 06:45 PM
<a href="http://www.dudes-japan.com" target="_blank">Dudes Japan</a>
#4
Posted 20 February 2008 - 07:00 PM
Click on this link to see my lovely google maps api in use
#5
Posted 25 March 2008 - 06:35 PM
Randommark, on Feb 21 2008, 09:00 AM, said:
Click on this link to see my lovely google maps api in use
Yes, Google Maps rules.
Have a question.
I have Google map on the site I`m currently working on but I`d like to change the degree of elevation, in other words give a more ZOOMED OUT look than it shows by default.
I`ve tried fiddling with MyMaps in Google itself but I cant get it to take effect on my web site. I thought maybe this was something that couldnt be changed.
Then I noticed that your map starts with a "whole of Britain" perspective.
Did you achieve this in Google Maps or is there a variable in the "object" tag that controls the degree of zooming in the maps display on your website.
Sorry if that sounds confusing.
#6
Posted 25 March 2008 - 09:20 PM
map.setCenter(new GLatLng( 35.40118, 139.57235), 16);
Change the '16' to something else. I believe 20 is zoomed in all the way, and lower numbers are zoomed out further, with 1 being the whole planet.
- I think.
<a href="http://www.dudes-japan.com" target="_blank">Dudes Japan</a>
#7
Posted 19 July 2008 - 03:03 PM
map.addOverlay(new GGeoXml('http://www.example.com/myplaces.kml'));
On your host, map that file to your scripting engine, for example:
RewriteRule ^myplaces.kml$ /myplaces.kml.php [L]
The let your code generate the right XML. I'm assuming a two-dimensional array here with a name and latitude/longitude.
// This code isn't complete, but should push you in the right direction.
foreach( $places as $place )
{
list( $name, $lat, $long ) = $place;
echo "<Placemark><name>". htmlspecialchars($name). "</name><coordinates>$long,$lat,0</coordinates></Placemark>\n";
}
#8
Posted 22 October 2008 - 03:28 AM
<?php
$foo="bar";
?>
<script>
window.open("myfile.htm?var=<?php print($foo)?>")
</script>
php runs on the server, javascript runs at the client. Thus the output from above will look like this:
<script>
window.open("myfile.htm?var=bar")
</script>
So... You can use php to output custom javascripts - it's a very useful method. Try this as an example:
<script>
<?php
for ($i=0;$i<5;$i++){
print("alert('hello world!');");
}
?>
</script>
The result is:
<script>
alert('hello world');
alert('hello world');
alert('hello world');
alert('hello world');
alert('hello world');
</script>
#9
Posted 24 November 2008 - 10:22 AM
#10
Posted 24 November 2008 - 10:33 AM
http://www.w3schools...stem_fwrite.asp
It would need a bit of modifying (depending on how complex you want to get) but it would work.
#11
Posted 24 November 2008 - 03:26 PM
I asked the question because I want to create an "admin" page for a site I'm building, and in it I want to allow the admin to change some JavaScript variables for functions in an external file. So the easiest thing to do would be to just place all my JavaScript that is influenced by the PHP in the PHP file and just keep the functions external, right? It would still work, but I would rather keep as much of my JavaScript external...
#12
Posted 24 November 2008 - 03:36 PM
http://www.php.net/fwrite
But yeh you could do that, or make the entire script on the fly. So depending who it is a different temporary javascript file is made.
Although you could just make a file named javascript.php and use 1 include. Then you can mess around with that javascript however you want (it would simplify things alot.)
The main reason for me of using external javascript on my websites is for SEO reasons. But seeing as its an admin panel, no bot will go near it and with so few connections to it server load shouldnt be a problem.
#13
Posted 25 November 2008 - 03:43 AM
But I understand the fwrite() function now, I should have looked for the explanation on php.net myself
#14
Posted 01 March 2011 - 12:46 PM


Help
This topic is locked
MultiQuote












