Welcome to the Cumulus Support forum.

Latest Cumulus MX V3 release 3.28.6 (build 3283) - 21 March 2024

Cumulus MX V4 beta test release 4.0.0 (build 4019) - 03 April 2024

Legacy Cumulus 1 release 1.9.4 (build 1099) - 28 November 2014
(a patch is available for 1.9.4 build 1099 that extends the date range of drop-down menus to 2030)

Download the Software (Cumulus MX / Cumulus 1 and other related items) from the Wiki

[Solved] Set php variable in Javascript

Other discussion about creating web sites for Cumulus that doesn't have a specific subforum

Moderator: daj

Post Reply
User avatar
beelzebubs
Posts: 62
Joined: Sun 04 Oct 2009 10:18 pm
Weather Station: Davis VP2
Operating System: Windows 7
Location: Sweden
Contact:

[Solved] Set php variable in Javascript

Post 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
Last edited by beelzebubs on Fri 02 Aug 2013 10:33 am, edited 1 time in total.
User avatar
steve
Cumulus Author
Posts: 26701
Joined: Mon 02 Jun 2008 6:49 pm
Weather Station: None
Operating System: None
Location: Vienne, France
Contact:

Re: Set php variable in Javascript

Post 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.
Steve
User avatar
beelzebubs
Posts: 62
Joined: Sun 04 Oct 2009 10:18 pm
Weather Station: Davis VP2
Operating System: Windows 7
Location: Sweden
Contact:

Re: Set php variable in Javascript

Post by beelzebubs »

Thanks Steve for the example but it was no luck.
/Kenneth
User avatar
steve
Cumulus Author
Posts: 26701
Joined: Mon 02 Jun 2008 6:49 pm
Weather Station: None
Operating System: None
Location: Vienne, France
Contact:

Re: Set php variable in Javascript

Post by steve »

Which bit doesn't work? Does the window.ss_lang variable get set correctly on the page?
Steve
User avatar
steve
Cumulus Author
Posts: 26701
Joined: Mon 02 Jun 2008 6:49 pm
Weather Station: None
Operating System: None
Location: Vienne, France
Contact:

Re: Set php variable in Javascript

Post 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.
You do not have the required permissions to view the files attached to this post.
Steve
User avatar
beelzebubs
Posts: 62
Joined: Sun 04 Oct 2009 10:18 pm
Weather Station: Davis VP2
Operating System: Windows 7
Location: Sweden
Contact:

Re: Set php variable in Javascript

Post 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
User avatar
beelzebubs
Posts: 62
Joined: Sun 04 Oct 2009 10:18 pm
Weather Station: Davis VP2
Operating System: Windows 7
Location: Sweden
Contact:

Re: Set php variable in Javascript

Post 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
You do not have the required permissions to view the files attached to this post.
User avatar
beteljuice
Posts: 3292
Joined: Tue 09 Dec 2008 1:37 pm
Weather Station: None !
Operating System: W10 - Threadripper 16core, etc
Location: Dudley, West Midlands, UK

Re: Set php variable in Javascript

Post 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.
Image
......................Imagine, what you will KNOW tomorrow !
User avatar
beelzebubs
Posts: 62
Joined: Sun 04 Oct 2009 10:18 pm
Weather Station: Davis VP2
Operating System: Windows 7
Location: Sweden
Contact:

Re: Set php variable in Javascript

Post 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.
User avatar
beelzebubs
Posts: 62
Joined: Sun 04 Oct 2009 10:18 pm
Weather Station: Davis VP2
Operating System: Windows 7
Location: Sweden
Contact:

Re: Set php variable in Javascript

Post 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
Post Reply