View Full Version : writing or recording javascript output?

12-30-2011, 09:32 PM
i have borrowed a java script to produce a weather forecast using data from my weather station.

//code to get your variables goes here
//I've assumed they are:
// $sager_wind = wind direction
// $sager_rumbo = wind change
// $sager_hpa = Baro value
// $sager_trend = Baro trend
// $sager_nubes = Sky condition

//now we can prepare them for the Sager script

//the sager script wants uppercase, so just to be sure...
$sager_wind = strtoupper($sager_wind);

//has to be uppercase
$sager_rumbo = strtoupper($sager_rumbo);

//allow for either of mB or Hg input
if ($sager_hpa > 50) { $sager_hpa = $sager_hpa * 0.0295300;}

//enumerate the Baro
switch ($sager_hpa) {

case ($sager_hpa > 30.4):
$sager_hpa = 1;

case ($sager_hpa > 30.1):
$sager_hpa = 2;

case ($sager_hpa > 29.9):
$sager_hpa = 3;

case ($sager_hpa > 29.7):
$sager_hpa = 4;

case ($sager_hpa > 29.5):
$sager_hpa = 5;

case (sager_hpa > 29.2):
$sager_hpa = 6;

case (sager_hpa > 28.:
$sager_hpa = 7;

case (sager_hpa > 28.0):
$sager_hpa = 8;


//enumerate the Baro trend
if (strcasecmp($sager_trend, 'Rising Rapidly') == 0) { $sager_trend = 1; }
if (strcasecmp($sager_trend, 'Rising Slowly') == 0) { $sager_trend = 2; }
if (strcasecmp($sager_trend, 'Steady') == 0) { $sager_trend = 3; }
if (strcasecmp($sager_trend, 'Falling Slowly') == 0) { $sager_trend = 4; }
if (strcasecmp($sager_trend, 'Falling Rapidly') == 0) { $sager_trend = 5; }

//enumerate the Sky condition
if (strcasecmp($sager_nubes, 'Clear') == 0) { $sager_nubes = 1; }
if (strcasecmp($sager_nubes, 'Partly Cloudy') == 0) { $sager_nubes = 2; }
if (strcasecmp($sager_nubes, 'Mostly Overcast') == 0) { $sager_nubes = 3; }
if (strcasecmp($sager_nubes, 'Overcast') == 0) { $sager_nubes = 4; }
if (strcasecmp($sager_nubes, 'Rain') == 0) { $sager_nubes = 5; }


<!-- standard header stuff goes here -->
<!-- and don't forget to include the sager script -->
<script src="sager_cast.js" type="text/javascript"></script>

<br />
Sager =
<span id="forecast">forecast will appear here</span>

<script type="text/javascript" language="javascript">

//we could do some fancy escaping to directly insert the php variables
//into the javascript function call, but this is harder to break.

z_wind = "<?php echo $sager_wind?>";
z_rumbo = "<?php echo $sager_rumbo?>";
z_hpa = <?php echo $sager_hpa?>;
z_trend = <?php echo $sager_trend?>;
z_nubes = <?php echo $sager_nubes?>;
document.getElementById('forecast').innerHTML = sager_cast( z_wind, z_rumbo, z_hpa, z_trend, z_nubes);


this basically turns my PHP variables intoa forecast by using the script above and also sager_cast.js (which crunches the numbers).

my question is how can i record the output to a text file

ie. this bit

<br />
Sager =
<span id="forecast">forecast will appear here</span>

i know how to run a cron job on my host server but whenever i try to use php to geta at it, it fails.

i understand that php and JS and side server / client server / host server different, but surely there must be a way to record the output using my host and the script that i have.

my goal is to catch the forecast every 15 mins (with a time stamp) and then look back at the (text) file to see how it has changed.

can anyone please help?

one suggestion is to turn the number crunching JS script into php so i can 'get at it' but there are thousands of lines of code!

thanks in advance, and hello from a newbie poster!

12-30-2011, 09:54 PM
if you use IE and you have your security settings low enough, you can use something like this:

<script language="javascript">

var files=0;

function WriteToFile() {
var date= new Date()
var fso = new ActiveXObject("Scripting.FileSystemObject");
var s = fso.CreateTextFile("C:\\Forecast "+files+".txt", true);
<body onLoad="WriteToFile()">


I don't know if there's a cross-browser method, but the above will create a new file (Forecast_1, Forecast_2, etc) every 15 minutes on your C drive - I'm guessing that if you want to change any of those things it's obvious enough how to do so

12-30-2011, 10:00 PM
thanks for the rapid help!

i didn't perhaps explain clearly enough that I'd like a forecast to be 'grabbed' 24/7 and not to my machine (which won't run 24/7).

I'd like a cron job to launch a script from my host server (GoDaddy) that somehow records the forecast at the point the script is run.

in PHP I would use fwrite commands to write PHP variables to a text file on my host server.

I don't know how to do this as my final output is in JS.

Hope that is clearer.

Old Pedant
12-30-2011, 10:08 PM
GoDaddy won't let you run scheduled tasks unless you have at least semi-dedicated hosting.

So you would have to invoke the code on GoDaddy from seme other computer. Which might mean that your home computer needs to run 24/7 just to hit GoDaddy every so often to do the updates.

Having said the above... If you are running on GoDaddy Linux, possibly they do allow CRON jobs. I doubt it, but at least you could ask them.

Having said that...

Why do you *need* to run 24/7 unless your web site is actually hit 24/7?

Why not simply record the time of the last update and, if a user hits your site and it has been more than an hour since the last update, you go and perform an update right then. The only "trick" to this will be ensuring that two users hitting your site at the same time don't both trigger an update.

12-30-2011, 10:15 PM
Yes i am using GoDaddy LInux and yes they DO allow cron jobs - i use them all the time.

i want to see how the forecast changes as my weather information changes so i'd like a regular forecast record to match the regulular weather variable record.

my issue is how do i save the forecast from JS?

12-30-2011, 10:23 PM
the forecast is in theory automatically updated every 1 minute - it is generated by the console and sent to the pc.

this is why i am keen to see it changing with the changing conditions - it's not as if it is run every 3hrs or so - it's minute by minute.

this will give me an insight into which factors from my weather station control it more than others.

Old Pedant
12-31-2011, 12:10 AM
You don't save from JS.

Oh, you could save some data on the user's machine, using either cookies or local data, but you can't save the data so it is visible to all users using JS.

To do that, you must use some kind of server-side coding. Since you are using Linux, that almost surely means you will need to use PHP to do this.

But you said "whenever i try to use php to geta at it, it fails". In what way does it fail?

It looks to me like you are saying that the only thing that fails, using PHP alone, is the call toe sager_cast( ). Is that right? Is so, that makes sense, since sager_cast() is JS code.

I suppose what you could do is perform the number crunching with JS and then use an AJAX call--or even a simple <form> post--to ask PHP to save the results?

Ugh...but that means you have to have JS running 24/7, as you said.

How hard would it be to read the JS code for sager_cast() and convert it to PHP??

12-31-2011, 07:16 AM
it fails as it saves the actual script code itself rather than the output.

the js code to convert is many many if statements.

see it at


Old Pedant
12-31-2011, 08:56 AM
it fails as it saves the actual script code itself rather than the output.

My head hurts. I have no idea why it would do that. But on the other hand, I have no idea how you could get PHP to execute JavaScript code.

It still looks to me like the answer is to use a browser to do the calculations and then send the results back to a PHP page to have it save them in a database or file.

12-31-2011, 09:09 AM
I used the php command file_get_contents


in a php script to grab the java output, but as i said it grabbed the entire code rather than the output.

Any other ideas? I can see that dead end fast approaching....

12-31-2011, 09:40 AM
You aren't understanding what he is telling you. Doing a file_get_contents on the JS file will not get you what you want. He said you need to use AJAX to post back to PHP the output of the JS call to sager_cast(). Don't do what post #2 said. There is no cross browser method. Again as stated you now have the problem of multiple users accessing the forecast at the same time. This isn't really an issue but you can have time stamps. The issue though is how often you will be doing the AJAX call back to your php script. Doing it every minute will take a lot of processing power from your server and slow down your load times quite a bit. Once an hour on a cron job is doable but PHP can't parse javascript. So what I would do is do the AJAX call back to PHP whenever a user accesses the weather report. Have your PHP check to see if the last file created is older than an hour. If it is, then create a new file, if not then don't do anything. In your AJAX call you will send post data back to your PHP processing script. This post data will be the output of your sager_cast() call e.g.

weather_output = sager_cast( z_wind, z_rumbo, z_hpa, z_trend, z_nubes);
// do your AJAX post here back to a php processing script.
document.getElementById('forecast').innerHTML = weather_output;

I don't know how much experience you have with PHP but this isn't going to be easy for a beginner. You may want to consider hiring a professional who can do this.

12-31-2011, 09:45 AM
you're right - i have no idea what to do now.

many thanks.

i have just got to grips with the basics of php, so taking on ajax myself may be tricky.