8 Replies - 1320 Views - Last Post: 26 March 2012 - 07:08 AM

#1 floppyspace  Icon User is offline

  • D.I.C Regular

Reputation: 48
  • View blog
  • Posts: 254
  • Joined: 04-February 10

php variable in javascript

Posted 26 March 2012 - 04:48 AM

Hi,

So I am working on a WordPress plugin and is near completion except for one detail.

WordPress allows you to save variables to a page in a plugin option menu which I have done but now I want to use the variable saved in a separate .js file.

so it is from $userChoice is saved (in a .php) and I want to use it as

jQuery().ready(function() {
	jQuery('body').loadit('$userChoice','wrapper');}


but I am lost as to how I can achieve this,google returned some info but nothing I was able to understand as relevant... can I use php variables in a separate .js file or are there other ways of achieving the same result? and help would be appreciated.

This post has been edited by floppyspace: 26 March 2012 - 04:52 AM


Is This A Good Question/Topic? 0
  • +

Replies To: php variable in javascript

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3550
  • View blog
  • Posts: 10,319
  • Joined: 08-June 10

Re: php variable in javascript

Posted 26 March 2012 - 05:20 AM

View Postfloppyspace, on 26 March 2012 - 01:48 PM, said:

can I use php variables in a separate .js file or are there other ways of achieving the same result?

simple answer: no.

complex answer: under certain circumstances, yes.

this would require:
- the .js being a .php file (that outputs JS)
- cross-page access to the value (e.g. via sessions or URL parameters)
Was This Post Helpful? 0
  • +
  • -

#3 floppyspace  Icon User is offline

  • D.I.C Regular

Reputation: 48
  • View blog
  • Posts: 254
  • Joined: 04-February 10

Re: php variable in javascript

Posted 26 March 2012 - 05:56 AM

so if I had the js in the header of my page and added a php variable <?php $userChoice?> would it work then? I will try this but not sure on the how.
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3550
  • View blog
  • Posts: 10,319
  • Joined: 08-June 10

Re: php variable in javascript

Posted 26 March 2012 - 06:10 AM

if the page is a .php file, probably. though the question is, do you really need to pass a PHP variable value to Javascript?
Was This Post Helpful? 0
  • +
  • -

#5 floppyspace  Icon User is offline

  • D.I.C Regular

Reputation: 48
  • View blog
  • Posts: 254
  • Joined: 04-February 10

Re: php variable in javascript

Posted 26 March 2012 - 06:50 AM

The reason is the javascript allows a user to enter a YouTube id and then will play the YouTube in the background of the page, hard coding it works fine, but I want this to be a plugin so anyone can do it easily.

Reason is I like the idea of animated background...

So idea is if a person creates a short video that repeats itself (waves lapping on a beach,wind in trees,stickman running across the bottom of a page) ,upload it to YouTube, gets the id and vola a simple animated background so to speak.

But I will try a change of approach.

Database, store 'id' and then call it to the head when the .php loads.

Not entirely sure if this will achieve anything different but will try loading the javascript in the head.php page with the variable been called to the $userChoice location on page (in the javascripts). Does this make sense???

So would something like this work, I am fairly new to js

<script>
jQuery().ready(function() {
jQuery('body').loadit('<?php $userChoice 
?>','wrapper');}
</script>


Was This Post Helpful? 0
  • +
  • -

#6 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3550
  • View blog
  • Posts: 10,319
  • Joined: 08-June 10

Re: php variable in javascript

Posted 26 March 2012 - 06:56 AM

View Postfloppyspace, on 26 March 2012 - 03:50 PM, said:

Reason is I like the idea of animated background...

though you like, many other people (including me) would not.
Was This Post Helpful? 0
  • +
  • -

#7 floppyspace  Icon User is offline

  • D.I.C Regular

Reputation: 48
  • View blog
  • Posts: 254
  • Joined: 04-February 10

Re: php variable in javascript

Posted 26 March 2012 - 07:02 AM

but if done correctly what would be wrong with it, I am not talking a race car travelling at 200miles an hour,more slow speed trees waving in background?

but yes we all have our own ideas on these things.

I did read up on this and you are right that it is not an easy task to get a php variable to javascript but it can be done the other way round easier (I think)?

This post has been edited by floppyspace: 26 March 2012 - 07:03 AM

Was This Post Helpful? 0
  • +
  • -

#8 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3550
  • View blog
  • Posts: 10,319
  • Joined: 08-June 10

Re: php variable in javascript

Posted 26 March 2012 - 07:03 AM

there’s nothing wrong with it, just personal preference (some just find a moving background just too distractive)
Was This Post Helpful? 0
  • +
  • -

#9 floppyspace  Icon User is offline

  • D.I.C Regular

Reputation: 48
  • View blog
  • Posts: 254
  • Joined: 04-February 10

Re: php variable in javascript

Posted 26 March 2012 - 07:08 AM

thank you, always good to hear others’ input.

grammar, my bad :P

This post has been edited by floppyspace: 26 March 2012 - 07:26 AM
Reason for edit:: grammar *g*

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1