Welcome Guest!

If you do not have an account yet on The Web Squeeze forums, please Register! It’s FREE and there are many benefits:

  • Receive Fast Advice
  • Learn Programming Languages
  • Get Professional Website Reviews
  • Quick Troubleshooting Assistance

> Passing Php Variables To Javascript

This is a discussion on Passing Php Variables To Javascript, within the PHP section. This forum and the thread "Passing Php Variables To Javascript" are both part of the Programming Your Website category.

 
Reply to this topicStart new topic
> Passing Php Variables To Javascript
MikeHopley
post Jul 14 2008, 10:20 AM
Post #1


Squeeze Machine
Group Icon

Posts: 682
Joined: 15-February 08
From: UK


I want to use some PHP variables in my javascript. How do I do this?
  • The PHP variable will be set only once on each page (so no worries about passing it back and forth!)
  • The javascript is an external .js file
  • I want to avoid document.write() if possible
  • Ideally, I would like to pass the PHP variable to a javascript function (as an argument, I suppose), to avoid creating a global JS variable and polluting the namespace. But that's not terribly important.
I'll also need to do some cleanup on the variable, but let's not worry about that for now.
Go to the top of the page
 
+Quote Post
MikeHopley
post Jul 14 2008, 11:24 AM
Post #2


Squeeze Machine
Group Icon

Posts: 682
Joined: 15-February 08
From: UK


Well, I seem to have worked it out:

CODE
$phpVar = "This variable was declared in PHP";
echo"<script type=\"text/javascript\">var jsVar = \"$phpVar\"</script>";


...but if anyone knows how to avoid declaring a global variable, let me know. That would be a nice coding-purity touch. wink.gif
Go to the top of the page
 
+Quote Post
c010depunkk
post Jul 14 2008, 02:28 PM
Post #3


Rapid Squeezer
Group Icon

Posts: 199
Joined: 14-February 08
From: Willich, Germany


Well, as long as the Javascript is inline, you can do what you did where ever you want, the variable don't have to be global...

CODE
<?php $php_var=34; ?>
<script type="text/javascript">
function bla() {
  var blubber=<?php echo($php_var); ?>;
}
</script>

This may be obvious, but i can't think of anything smarter at the moment... or i misunderstood the question.... (most likely the latter).


--------------------
www.c010depunkk.com ~ the hangout of a web developer
Go to the top of the page
 
+Quote Post
Monie
post Jul 14 2008, 08:09 PM
Post #4


Squeeze Machine
*****

Posts: 733
Joined: 13-February 08
From: Borneo


Yeah I remember learning this trick with Rakuli smile.gif


--------------------

Go to the top of the page
 
+Quote Post
Rakuli
post Jul 14 2008, 08:28 PM
Post #5


Squeeze Machine
Group Icon

Posts: 766
Joined: 13-February 08
From: Catching the squeezed drips downunder.


If you wanted, you could set up a factory function on your external js file

CODE
function makeAnObjec(give, me, some args)
{
     this.give = give;
     this.me = me;
     this.some = some;
     this.args = args;
}

And if you already are declaring some variables inside of your external .js file, it's just a matter of adding a property when you write it out with Pph

CODE
alreadydeclaredVariable.phpPassedProperties = new makeAnObject(<?php echo '"give", "me", "some", "args"; ?>);


So now you are using an object that is already in existence by creating another object on page load -- no namespace issues arising apart from those that are already present.


--------------------
Luke Dingle . com

Turn Over a Playful Leaf on Web Design -- read about the javascript cat
Go to the top of the page
 
+Quote Post
Monie
post Jul 14 2008, 08:39 PM
Post #6


Squeeze Machine
*****

Posts: 733
Joined: 13-February 08
From: Borneo


Oh this is beyond my brain smile.gif Hehehe...


--------------------

Go to the top of the page
 
+Quote Post
c010depunkk
post Jul 15 2008, 12:40 AM
Post #7


Rapid Squeezer
Group Icon

Posts: 199
Joined: 14-February 08
From: Willich, Germany


QUOTE (Rakuli @ Jul 15 2008, 03:28 AM) *
If you wanted, you could set up a factory function on your external js file

See, there's the clever answer I was looking for wink.gif


--------------------
www.c010depunkk.com ~ the hangout of a web developer
Go to the top of the page
 
+Quote Post
MikeHopley
post Jul 15 2008, 02:41 AM
Post #8


Squeeze Machine
Group Icon

Posts: 682
Joined: 15-February 08
From: UK


QUOTE (Rakuli @ Jul 15 2008, 02:28 AM) *
If you wanted, you could set up a factory function on your external js file

CODE
function makeAnObjec(give, me, some args)
{
      this.give = give;
      this.me = me;
      this.some = some;
      this.args = args;
}


Ooooh, this looks interesting. I might need some more guidance on this later, but first I should play with it and see what happens. smile.gif

QUOTE
And if you already are declaring some variables inside of your external .js file, it's just a matter of adding a property when you write it out with Pph


Except I'm not (I only declare local variables; when I need a global variable, which is rarely, I create a function instead). So how would I access this factory data without attaching it to a global variable?

By the way, is that a good idea? For example, I had a global variable that checks for IE:

CODE
// cunning browser detect, using conditional comments
var x = document.getElementById("browserIE"); if (x) { var ie = 1 }


...and I encapsulated it in a function instead (to get rid of the global):

CODE
function ie() {         // cunning browser detect, using conditional comments
    var x = document.getElementById("browserIE"); if (x) { return 1 }
}


...and then I test for if(ie()==1) instead of testing for if(ie).
Go to the top of the page
 
+Quote Post
Rakuli
post Jul 15 2008, 07:34 AM
Post #9


Squeeze Machine
Group Icon

Posts: 766
Joined: 13-February 08
From: Catching the squeezed drips downunder.


No, the slowest thing to do in Javascript, meaning the largest drain on client side resources by Javascript, is a function call. Javascript is an OOP language but it is not so to the point that complete abstraction is possible -- or the best solution.

If your namespace only has one object thats properties are objects, this is okay. Speed on todays computers isn't a huge problem but why use extra resources if you don't have to? If you only need to run a function once, it's probably best not to declare it as a function and instead create an object with the settings. This will save you some bandwidth as well (it's usually shorter to compare against variables [ if (ie) ] than functions [ if (ie()) ]


--------------------
Luke Dingle . com

Turn Over a Playful Leaf on Web Design -- read about the javascript cat
Go to the top of the page
 
+Quote Post
MikeHopley
post Jul 15 2008, 08:48 AM
Post #10


Squeeze Machine
Group Icon

Posts: 682
Joined: 15-February 08
From: UK


QUOTE (Rakuli @ Jul 15 2008, 01:34 PM) *
No, the slowest thing to do in Javascript, meaning the largest drain on client side resources by Javascript, is a function call. Javascript is an OOP language but it is not so to the point that complete abstraction is possible -- or the best solution.

If your namespace only has one object thats properties are objects, this is okay. Speed on todays computers isn't a huge problem but why use extra resources if you don't have to? If you only need to run a function once, it's probably best not to declare it as a function and instead create an object with the settings. This will save you some bandwidth as well (it's usually shorter to compare against variables [ if (ie) ] than functions [ if (ie()) ]


Let me check whether I've understood this correctly:
  • Instead of using LOTS of global variables, it's better to create one object and store the "variables" inside that.
  • This object should have a distinctive name, to make namespace conflicts improbable (e.g. BadmintonBibleObject).
  • This is a better solution than attempting "complete abstraction" by encapsulating everything in functions, because function calls are much, much slower.
Is that correct?
Go to the top of the page
 
+Quote Post
Rakuli
post Jul 15 2008, 08:56 AM
Post #11


Squeeze Machine
Group Icon

Posts: 766
Joined: 13-February 08
From: Catching the squeezed drips downunder.


In a nutshell that is correct.

Point one I can't confirm any further than saying that having one global packed with properties and avoiding potential namespace conflicts would be a better choice. The second and third points are logical and correct respectively.


--------------------
Luke Dingle . com

Turn Over a Playful Leaf on Web Design -- read about the javascript cat
Go to the top of the page
 
+Quote Post
MikeHopley
post Jul 15 2008, 09:05 AM
Post #12


Squeeze Machine
Group Icon

Posts: 682
Joined: 15-February 08
From: UK


QUOTE (Rakuli @ Jul 15 2008, 02:56 PM) *
Point one I can't confirm any further than saying that having one global packed with properties and avoiding potential namespace conflicts would be a better choice.


So that one global could also be something other than an object? Excuse my denseness; I'm picking up the terminology as I go (I'm pretty sketchy on what an object is, having never used one).

Thanks for all this advice; I always seem to learn something fundamentally new when you answer one of my threads. smile.gif Beyond the expediency of solving an immediate problem, I also enjoy learning something new.
Go to the top of the page
 
+Quote Post
Rakuli
post Jul 15 2008, 09:13 AM
Post #13


Squeeze Machine
Group Icon

Posts: 766
Joined: 13-February 08
From: Catching the squeezed drips downunder.


Well an object is just the 'whole'.. It can be made up of properties and methods.

The global would either be an object or an array of objects or array of variables -- the same principle really; storing all global data under one namespace.

An object in javascript is like an object in PHP or python except that in javascript there is are more ways to create them. $ in jQuery is just an object and it stores everything it needs, all functions and properties to do everything it does.


--------------------
Luke Dingle . com

Turn Over a Playful Leaf on Web Design -- read about the javascript cat
Go to the top of the page
 
+Quote Post
MikeHopley
post Jul 15 2008, 09:21 AM
Post #14


Squeeze Machine
Group Icon

Posts: 682
Joined: 15-February 08
From: UK


Gotcha. Thanks.

It's my own fault for never learning to program "properly"; I just picked stuff up as I went along. This is actually quite an efficient style of learning, but it does leave dangerous gaps in my knowledge.

Thankfully, you help me fill in the gaps. smile.gif
Go to the top of the page
 
+Quote Post
Rakuli
post Jul 15 2008, 09:26 AM
Post #15


Squeeze Machine
Group Icon

Posts: 766
Joined: 13-February 08
From: Catching the squeezed drips downunder.


QUOTE (MikeHopley @ Jul 16 2008, 12:21 AM) *
Gotcha. Thanks.

It's my own fault for never learning to program "properly"; I just picked stuff up as I went along. This is actually quite an efficient style of learning, but it does leave dangerous gaps in my knowledge.

Thankfully, you help me fill in the gaps. smile.gif



LOL, no probelm.


--------------------
Luke Dingle . com

Turn Over a Playful Leaf on Web Design -- read about the javascript cat
Go to the top of the page
 
+Quote Post
If you found The Web Squeeze to be helpful, please donate so we can keep this site FREE, FRESH, and fortified with Web Design & Development info!
Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Collapse

> Similar Topics

    Topic Title Replies Topic Starter Views Last Action
No New Posts   2 marSoul 355 23rd February 2008 - 10:52 AM
Last post by: marSoul
No New Posts   2 Jason 186 3rd April 2008 - 05:57 AM
Last post by: Jason
No New Posts   13 Jason 403 17th April 2008 - 12:55 AM
Last post by: Ryan
No New Posts   2 MikeHopley 201 25th April 2008 - 09:14 AM
Last post by: MikeHopley
No New Posts   6 Jason 331 2nd June 2008 - 04:35 AM
Last post by: Monie