Page 1 of 1

Run PHP from setTimeout

Posted: Tue Sep 09, 2003 12:57 am
by jcoppo
Can someone explain why executing PHP code from a setTimeout loop does not run after the first call to the function. As an example, the following code will display a random number the first time it is called. But successive calls will not change the PHP output. I know that it is running because the javascript variable Cnt increments ever 1000 milliseconds.

Code: Select all

<HTML><HEAD><TITLE>Test</TITLE>
<SCRIPT type=text/javascript>
var Cnt=0;
function showtime()&#123;
	timerID = setTimeout("showtime()",1000)
	var OUT = document.getElementById('OUT');
	var n = '<?php echo intval(rand(1,20)); ?>';
	Cnt++;
	OUT.innerHTML=n+' '+Cnt;
&#125;
</SCRIPT>
</HEAD>
<BODY onLoad="showtime()">
<DIV id=OUT></DIV>
</BODY></HTML>
I have many situations where I would like to use this technique. For instance, dynamically updating market quotes and displaying news feeds.

Any help would be most appreciated.
jcoppo

Posted: Tue Sep 09, 2003 2:13 am
by JAM
When the javascript is run, it's allready sendt to the clients browser. Then its to late to run any php-scripts as php is server based.

There is a sticky topic in this forum regarding php and javascript working together.

viewtopic.php?t=1030

Posted: Tue Sep 09, 2003 7:20 am
by jcoppo
That clears it up. You saved me endless hours of frustration.

jcoppo

Posted: Tue Sep 09, 2003 9:09 am
by liljester
javascript can refresh a page using setTimeout. so if you have an iframe (or frame for that matter) with say a stock quote, a javascipt could refresh the iframe for you.