Hi,
I'm stuck, but it's almost working!
From a html page, my javascript calls a server-side php script. The php reads a value from a server-side .txt file and passes it back as a javascript variable. This all works fine.
However, I want it to loop within a javascript function every 5 seconds. The function does other stuff, so looping within this function is essential (I've simplified it in the example below).
Any advice would be appreciated, I don't want to learn Ajax quite yet as my brain's already full with trying to learn php/javascript!
I've got the following in my .txt file:
This gets pulled into an array by my .php code:
[php]
<?php
Header("content-type: application/x-javascript");
//read html param to get graph_number
$graph_number = $_GET["graph_requ est"];
//open datafile and create array of each line
$fp=fopen('C:\P rogram Files\Apache Software Foundation\Apac he2.2\htdocs\fr ame_realtime.tx t',"r");
while(!feof($fp )) {
$line=fgets($fp );
list($key,$valu e) = split("[[:space:]]+",$line);
$array[$key] = $value;
}
fclose($fp);
//send output back to calling page
echo 'var random_number=" '.$array[$graph_number] .'";';
?>
[/php]
Finally, I've got the following in my .html page, which calls the .php:
[html]
<html>
<head>
<script type="text/javascript" src="realtime_r ead_values.php? graph_request=g raph_counter1"> </script>
<script type="text/javascript">
function AddBar1()
{
document.write( random_number);
setTimeout('Add Bar1()', 5000);
}
</script>
</head>
<body onload="AddBar1 ()">
</body>
</html>
[/html]
(Ignore the gap between count and err1 in this last example - the form is playing games..)
This prints the number '81' on my html page, and works fine for the 1st load. After this is doesn't go through the loop anymore so the number remains the same. I've tried many ways to get the php call into the loop but it won't work.
I need help in working out how to put the .php call INSIDE the looping function.
thanks,
Will
I'm stuck, but it's almost working!
From a html page, my javascript calls a server-side php script. The php reads a value from a server-side .txt file and passes it back as a javascript variable. This all works fine.
However, I want it to loop within a javascript function every 5 seconds. The function does other stuff, so looping within this function is essential (I've simplified it in the example below).
Any advice would be appreciated, I don't want to learn Ajax quite yet as my brain's already full with trying to learn php/javascript!
I've got the following in my .txt file:
Code:
graph_counter1 81
[php]
<?php
Header("content-type: application/x-javascript");
//read html param to get graph_number
$graph_number = $_GET["graph_requ est"];
//open datafile and create array of each line
$fp=fopen('C:\P rogram Files\Apache Software Foundation\Apac he2.2\htdocs\fr ame_realtime.tx t',"r");
while(!feof($fp )) {
$line=fgets($fp );
list($key,$valu e) = split("[[:space:]]+",$line);
$array[$key] = $value;
}
fclose($fp);
//send output back to calling page
echo 'var random_number=" '.$array[$graph_number] .'";';
?>
[/php]
Finally, I've got the following in my .html page, which calls the .php:
[html]
<html>
<head>
<script type="text/javascript" src="realtime_r ead_values.php? graph_request=g raph_counter1"> </script>
<script type="text/javascript">
function AddBar1()
{
document.write( random_number);
setTimeout('Add Bar1()', 5000);
}
</script>
</head>
<body onload="AddBar1 ()">
</body>
</html>
[/html]
(Ignore the gap between count and err1 in this last example - the form is playing games..)
This prints the number '81' on my html page, and works fine for the 1st load. After this is doesn't go through the loop anymore so the number remains the same. I've tried many ways to get the php call into the loop but it won't work.
I need help in working out how to put the .php call INSIDE the looping function.
thanks,
Will
Comment