Page 1 of 1

[Solved] Set php variable in Javascript

Posted: Thu 01 Aug 2013 7:43 pm
by beelzebubs
Is it possible to get language variable from php and set that variable in a external javascript file?

That I have try to do is to have one js file to both Swedish and English.
In steel series gauges you have to chose to have one language / js file.
I want to use the variable that I all ready have in a php file an set that in the js file, but I have not been able to get it to work.

In the gauges.js it look like this from the beginning.

Code: Select all

var gauges = (function () {
    var strings = LANG.SE,         //Set to your default language. Store all the strings in one object
In the php file I have done like this:
First I change value to the right format

Code: Select all

if ($lang=="sv")
  {
  $ss_lang="LANG.SE";
  }
  else
  {
  $ss_lang="LANG.EN";
  }
And if I understand things right I can use this to export the value to the js file.

Code: Select all

<script>var ss_lang = '<?php echo $ss_lang; ?>';</script>
<script src="lib/steelseries/gauges.js"></script>
But how do I get it in to the gauges.js and get it to work?
I guess I need to change this

Code: Select all

var strings = LANG.SE
to some thing else but to what?

Maybe someone here now the right solution and can give me some help.

(I have no example up and running at my web page yet, it in experimental stage and runs on a local computer)

/Kenneth

Re: Set php variable in Javascript

Posted: Thu 01 Aug 2013 8:08 pm
by steve
You can declare a 'global' javascript variable like this:

Code: Select all

<script>window.ss_lang = '<?php echo $ss_lang; ?>';</script>
<script src="lib/steelseries/gauges.js"></script>
and then use it in gauges.js like this:

Code: Select all

var strings = window.ss_lang
Notes:

1. I haven't tried this and I might be making it all up.
2. Even if it does work, there may be other/better ways of doing it.

Re: Set php variable in Javascript

Posted: Thu 01 Aug 2013 8:30 pm
by beelzebubs
Thanks Steve for the example but it was no luck.
/Kenneth

Re: Set php variable in Javascript

Posted: Thu 01 Aug 2013 8:33 pm
by steve
Which bit doesn't work? Does the window.ss_lang variable get set correctly on the page?

Re: Set php variable in Javascript

Posted: Thu 01 Aug 2013 8:48 pm
by steve
I created a PHP file with the following in it

Code: Select all

<?php
$ss_lang="LANG.SE";
?>

<script>window.ss_lang = '<?php echo $ss_lang; ?>';</script>
<script src="test.js"></script>
and a javascript file test.js with the following in it

Code: Select all

var strings = window.ss_lang;
alert('strings = '+strings);
When the page is invoked, the result is this:
2013-08-01 21_43_57-nybbles.co.uk_test.php.png
You can see it working at http://nybbles.co.uk/test.php

So unless I've misunderstood what it is you're trying to do, I don't understand why that doesn't work for you.

Re: Set php variable in Javascript

Posted: Thu 01 Aug 2013 9:01 pm
by beelzebubs
steve wrote:Which bit doesn't work? Does the window.ss_lang variable get set correctly on the page?
The php page is set correct but there something with wrong in js file.

If I built a new js file (test.js) and in that use

Code: Select all

document.write(ss_lang);
I can see LANG.SE or LANG.EN on the php page.
To me it looks like the gauges.js don't understand the new variable.

/Kenneth

Re: Set php variable in Javascript

Posted: Thu 01 Aug 2013 9:10 pm
by beelzebubs
steve wrote:
So unless I've misunderstood what it is you're trying to do, I don't understand why that doesn't work for you.
Please have a look at the file I upload.

In the top of that you define which languages you want to use.
Here I want to use the variable I get from the php file.

/Kenneth

Re: Set php variable in Javascript

Posted: Thu 01 Aug 2013 10:16 pm
by beteljuice
I guess I need to change this

Code: Select all

var strings = LANG.SE
Look again at the code.
It's written in object style.

Code: Select all

var strings = LANG.SE, // COMMA the var strings continues below this
So you need to have an extra 'replaceable' name

Code: Select all

var strings = MYLANG, // COMMA the var strings continues below this
Then use the ideas above to 'rename' MYLANG.

Did you know you can have <script src="js/mycode.php" type="text/javascript"></script>
and just put the tiny php bit (with <?php ?> tags) where required.

Re: Set php variable in Javascript

Posted: Fri 02 Aug 2013 10:20 am
by beelzebubs
beteljuice wrote:
I guess I need to change this

Code: Select all

var strings = LANG.SE
Look again at the code.
It's written in object style.

Code: Select all

var strings = LANG.SE, // COMMA the var strings continues below this
So you need to have an extra 'replaceable' name

Code: Select all

var strings = MYLANG, // COMMA the var strings continues below this
Then use the ideas above to 'rename' MYLANG.

Did you know you can have <script src="js/mycode.php" type="text/javascript"></script>
and just put the tiny php bit (with <?php ?> tags) where required.
Thanks for your help beteljuice.
But I got another thats work out of the box.

And no I didn't no that tell the browser that you have a javascript and put some other script there instead.

Re: Set php variable in Javascript

Posted: Fri 02 Aug 2013 10:32 am
by beelzebubs
It look like the right solution is to add a sting to the page.

Code: Select all

<script>changeLang(<?php echo $ss_lang; ?>);</script>
Some one has noticed my post here and was were kindly to send me a mail with a solution.
And now everything works that I wanted.

Thanks everyone!!
/Kenneth