Welcome to the Cumulus Support forum.

Latest Cumulus MX V4 release 4.4.2 (build 4085) - 12 March 2025

Latest Cumulus MX V3 release 3.28.6 (build 3283) - 21 March 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

If you are posting a new Topic about an error or if you need help PLEASE read this first viewtopic.php?p=164080#p164080

Javascript update question

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

Moderator: daj

Post Reply
fractonimbus
Posts: 159
Joined: Thu 03 Feb 2011 1:15 am
Weather Station: WH1091
Operating System: Windows 7 on a Dell Vostro
Location: Canberra

Javascript update question

Post by fractonimbus »

I have a PHP file whose function is to draw a graph (PNG format, 700x360 pixels). It loads the data and plots a new graph every time it is called.

Given how the experts here are averse to using meta refresh to update pages ;-), I want to use Javascript to update the PHP-generated image in an HTML page every 60 seconds or so.

What's the simplest way to do that, given that I'm using jquery.js (and wc.js) to update the "page last updated" entry in the html page already. How do I get Javascript to call jquery to get it to call the PHP file? (if that's what I should be doing?)

Thanks

DN

The meta refreshed page is here: http://www.deakinwx.com/wx/stemp.htm
Image
fractonimbus
Posts: 159
Joined: Thu 03 Feb 2011 1:15 am
Weather Station: WH1091
Operating System: Windows 7 on a Dell Vostro
Location: Canberra

Re: Javascript update question

Post by fractonimbus »

More on the problem: What I've struck (and trying more than one method to replace the object id with the updated PHP output) is that if I add the id to the IMG tag (holding the PNG generated by PHP), nothing happens. If I put the <img..> inside <p> tags and add the id to the <p> tag, it does replace the image but as a text dump of gibberish characters, ie the PNG file treated as text. So I suspect I have a mime-type problem. Does anyone have an idea how to handle this?

(FWIW, I did try seeding the replacement file with a random suffix: xxx.php?random - it generates the PHP-constructed image when entered directly, but the Javascript doesn't replace anything. Anyway, I may not need to do this, as the PHP creates a new image every time it is called)

I may try getting the PHP to explicitly generate a file type header as well as the PNG and see what happens. When I load the current PHP generated image directly into a browser, it displays quite happily, so I am bit confused as to what's going on.

DN
fractonimbus
Posts: 159
Joined: Thu 03 Feb 2011 1:15 am
Weather Station: WH1091
Operating System: Windows 7 on a Dell Vostro
Location: Canberra

Re: Javascript update question

Post by fractonimbus »

A bit more: with the id in the <p> tag, the update does occur, but apparently not when it's in the <img> tag.

Very odd (BTW, I'm using PHPlot to create the graph).

DN
Image
DaveNZ
Posts: 373
Joined: Mon 07 Dec 2009 10:27 pm
Weather Station: Davis VP2
Operating System: Windows 7 64-bit
Location: Howick, Auckland, New Zealand
Contact:

Re: Javascript update question

Post by DaveNZ »

I would use a javascript like the following. It should work, though I haven't tested it...
I haven't really learnt much jQuery so tend to write javascript instead.



<script type="text/javascript">

function timer() {
var interval=60; // seconds
var wait=interval*1000;
setTimeout("updateImg()",wait)
}

function updateImg() {
var imgspan=document.getElementById('graph_container');
var d=new Date();
var src="../scw.php?"+d.getTime();
imgspan.innerHTML='<img src="'+src+'" alt="some text" />';
timer();
}


</script>

<body onload="timer()">


Then for the image:

<span id="graph_container">
<img src="../scw.php" alt="some text" />
</span>
fractonimbus
Posts: 159
Joined: Thu 03 Feb 2011 1:15 am
Weather Station: WH1091
Operating System: Windows 7 on a Dell Vostro
Location: Canberra

Re: Javascript update question

Post by fractonimbus »

Thanks, Dave, that works! http://www.deakinwx.com/wx/skytemp.htm

DN
Image
Post Reply