Jump to content

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.

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

Passing PHP variables into Javascript Rate Topic: -----

#1 User is offline   Randommark 

  • W.R. Private
  • Group: Members
  • Posts: 46
  • Joined: 05-February 08
  • Gender:Male
  • Location:Portsmouth, Englanshire

Post icon  Posted 20 February 2008 - 01:28 PM

Title says it all, how do I do this?

I have some longitude and latitude data in my database and want to use the google maps api to display them.
0

#2 User is offline   Randommark 

  • W.R. Private
  • Group: Members
  • Posts: 46
  • Joined: 05-February 08
  • Gender:Male
  • Location:Portsmouth, Englanshire

Posted 20 February 2008 - 01:32 PM

I always do this, speak to fast. 2 minutes after posting the topic, I have the answer. Ill post what I found as well, incase this helps someone as well.

<html>
<?php
$MyVar3 = "Something in PHP";
?>
<script language="JavaScript">
document.write(<?php print "'".$MyVar3."'"; ?>);
</script>
</html>

0

#3 User is offline   haku 

  • 日本語 Ninja
  • Group: Members
  • Posts: 652
  • Joined: 21-September 07
  • Gender:Male
  • Location:Yokohama, Japan

Posted 20 February 2008 - 06:45 PM

Exactly. Just don't try it the other way - passing javascript variables to PHP. Won't work. Has to be done through either a $_GET variable or a cookie.
<a href="http://www.jaypan.com" target="_blank">Jaypan</a>
<a href="http://www.dudes-japan.com" target="_blank">Dudes Japan</a>
0

#4 User is offline   Randommark 

  • W.R. Private
  • Group: Members
  • Posts: 46
  • Joined: 05-February 08
  • Gender:Male
  • Location:Portsmouth, Englanshire

Posted 20 February 2008 - 07:00 PM

Yeh thanks, luckily I dont need to that yet.

Click on this link to see my lovely google maps api in use :D http://uni-portal.co...y-of-Portsmouth
0

#5 User is offline   Beavis 

  • W.R. Corporal
  • Group: Members
  • Posts: 167
  • Joined: 24-March 08

Posted 25 March 2008 - 06:35 PM

View PostRandommark, on Feb 21 2008, 09:00 AM, said:

Yeh thanks, luckily I dont need to that yet.

Click on this link to see my lovely google maps api in use :D http://uni-portal.co...y-of-Portsmouth



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.
0

#6 User is offline   haku 

  • 日本語 Ninja
  • Group: Members
  • Posts: 652
  • Joined: 21-September 07
  • Gender:Male
  • Location:Yokohama, Japan

Posted 25 March 2008 - 09:20 PM

I put google maps in a page I created a year ago, so its been a while, but I'm pretty sure I adjusted the zoom level in this line of code:

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.jaypan.com" target="_blank">Jaypan</a>
<a href="http://www.dudes-japan.com" target="_blank">Dudes Japan</a>
0

#7 User is offline   Rob Kaper 

  • W.R. Corporal
  • Group: Members
  • Posts: 97
  • Joined: 19-July 08
  • Gender:Male
  • Location:Rotterdam, NLD

Posted 19 July 2008 - 03:03 PM

For use with Google Maps, I recommend creating a KML file, especially when you have large or dynamic datasets. Benefit: you can add your .kml file as network link to Google Earth as well.

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";
}

Rob Kaper - Rotterdam
0

#8 User is offline   Richard123 

  • W.R. Private
  • Group: Members
  • Posts: 1
  • Joined: 22-October 08

Posted 22 October 2008 - 03:28 AM

Hi dear,
<?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>

0

#9 User is offline   temhawk 

  • W.R. Private First-Class
  • Group: Members
  • Posts: 322
  • Joined: 30-August 07
  • Gender:Male
  • Interests:travel, cg art, macs, music, skateboarding, programming, discovery channel, TextMate 2

Posted 24 November 2008 - 10:22 AM

Hello, is there a way to work with PHP in external JavaScript files?
0

#10 User is offline   sypher 

  • the owner3r
  • Group: Administrators
  • Posts: 1,578
  • Joined: 04-April 06
  • Location:North Wales, UK
  • Interests:Art, Boxing, MMA, Graphic Design, Web Design etc. ;)

Posted 24 November 2008 - 10:33 AM

Hm i think so using fwrite()
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.
sypher design - North Wales Web Design | Latest Work: - Scala Cinema

CSS - Can't See Sh*t
0

#11 User is offline   temhawk 

  • W.R. Private First-Class
  • Group: Members
  • Posts: 322
  • Joined: 30-August 07
  • Gender:Male
  • Interests:travel, cg art, macs, music, skateboarding, programming, discovery channel, TextMate 2

Posted 24 November 2008 - 03:26 PM

Hmm, the example on W3's site doesn't make sense. Why would it output "21"? It looks like it's the length of the file's contents rather than the contents...

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... ;)
0

#12 User is offline   sypher 

  • the owner3r
  • Group: Administrators
  • Posts: 1,578
  • Joined: 04-April 06
  • Location:North Wales, UK
  • Interests:Art, Boxing, MMA, Graphic Design, Web Design etc. ;)

Posted 24 November 2008 - 03:36 PM

Heres a more indepth analysis
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.
sypher design - North Wales Web Design | Latest Work: - Scala Cinema

CSS - Can't See Sh*t
0

#13 User is offline   temhawk 

  • W.R. Private First-Class
  • Group: Members
  • Posts: 322
  • Joined: 30-August 07
  • Gender:Male
  • Interests:travel, cg art, macs, music, skateboarding, programming, discovery channel, TextMate 2

Posted 25 November 2008 - 03:43 AM

Cool, didn't know that you could use PHP files as the source for an external JavaScript file. I think I'll go with that :)

But I understand the fwrite() function now, I should have looked for the explanation on php.net myself ^_^
0

#14 User is offline   EncoderDecoder 

  • W.R. Sergeant
  • Group: Members
  • Posts: 260
  • Joined: 01-March 11
  • Gender:Female
  • Location:Philippines

Posted 01 March 2011 - 12:46 PM

why not pass the php variable into a hidden input type in a form and submit it to javascript..?
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users