Connecting Tech Pros Worldwide Forums | Help | Site Map

Javascript calling PHP into variable - almost working. Can you help?

Newbie
 
Join Date: Sep 2008
Posts: 7
#1: Sep 11 '08
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:

Expand|Select|Wrap|Line Numbers
  1. graph_counter1 81
  2.  
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_request"];
//open datafile and create array of each line
$fp=fopen('C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\frame_realtime.txt',"r ");
while(!feof($fp)) {
$line=fgets($fp);
list($key,$value) = 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_read_values.php?graph_request=graph_ counter1"></script>
<script type="text/javascript">
function AddBar1()
{
document.write(random_number);
setTimeout('AddBar1()', 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

acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#2: Sep 11 '08

re: Javascript calling PHP into variable - almost working. Can you help?


Give your script tag an ID and set its src to the PHP file:
Expand|Select|Wrap|Line Numbers
  1. document.getElementById("scriptID").src = "realtime_read_values.php?graph_request=graph_counter1";
Newbie
 
Join Date: Sep 2008
Posts: 7
#3: Sep 11 '08

re: Javascript calling PHP into variable - almost working. Can you help?


Thanks very much acoder. Do you mean the 1st script tag or the 2nd - i.e. do I remove the 1st script completely and add the tag to the 2nd?

Here's what I have now, but I know it's wrong:

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script type="text/javascript" id='scriptID'></script>
  4. <script type="text/javascript">  
  5. function AddBar1()
  6. {
  7. document.getElementById("scriptID").src = "realtime_read_values.php?graph_request=graph_counter1";
  8. document.write(random_number);
  9. setTimeout('AddBar1()', 5000);
  10. }
  11. </script>
  12. </head>
  13. <body onload="AddBar1()">
  14. </body>
  15. </html>
  16.  
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#4: Sep 11 '08

re: Javascript calling PHP into variable - almost working. Can you help?


I meant the first one. Keep the src set as you had earlier.

A note on document.write. Don't use it after page load. It opens the document for writing. Use something like this instead:
Expand|Select|Wrap|Line Numbers
  1. var txt = document.createTextNode(" some text ");
  2. document.body.appendChild(txt);
Newbie
 
Join Date: Sep 2008
Posts: 7
#5: Sep 11 '08

re: Javascript calling PHP into variable - almost working. Can you help?


It's almost there, now it's repeatedly printing the number (you were right, it was just printing it on page load with my last attempt). However, when I change the text file from:

Expand|Select|Wrap|Line Numbers
  1. graph_counter1 81
  2.  
to

Expand|Select|Wrap|Line Numbers
  1. graph_counter1 82
  2.  
it doesn't update on my html page. Any thoughts?

Thanks again for helping me.

Here's what I have now, after your suggestions:

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script type="text/javascript" id="scriptID" src="realtime_read_values.php?graph_request=graph_counter1"></script>
  4. <script type="text/javascript">  
  5. function AddBar1()
  6. {
  7. document.getElementById("scriptID").src = "realtime_read_values.php?graph_request=graph_counter1";
  8. var txt = document.createTextNode(random_number);
  9. document.body.appendChild(txt);
  10. setTimeout('AddBar1()', 5000);
  11. }
  12. </script>
  13. </head>
  14. <body onload="AddBar1()">
  15. </body>
  16. </html>
  17.  
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#6: Sep 12 '08

re: Javascript calling PHP into variable - almost working. Can you help?


If you try running the PHP page on its own, does it change when you change the text file?
Newbie
 
Join Date: Sep 2008
Posts: 7
#7: Sep 12 '08

re: Javascript calling PHP into variable - almost working. Can you help?


Yes it does.

I found it's calling the page once within the loop, then using that value each time. I tested this by waiting for the response from the 1st loop, then commenting out the .php page and saving it. The loop carrried on unaffected.
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#8: Sep 12 '08

re: Javascript calling PHP into variable - almost working. Can you help?


It may be caching the page. Try:
Expand|Select|Wrap|Line Numbers
  1. document.getElementById("scriptID").src = "realtime_read_values.php?graph_request=graph_counter1&d=" + (new Date()).getTime();
Newbie
 
Join Date: Sep 2008
Posts: 7
#9: Sep 13 '08

re: Javascript calling PHP into variable - almost working. Can you help?


This works, thanks so much.
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#10: Sep 13 '08

re: Javascript calling PHP into variable - almost working. Can you help?


You're welcome :)
Reply