473,382 Members | 1,658 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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

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
Sep 11 '08 #1
9 2999
acoder
16,027 Expert Mod 8TB
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";
Sep 11 '08 #2
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.  
Sep 11 '08 #3
acoder
16,027 Expert Mod 8TB
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);
Sep 11 '08 #4
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.  
Sep 11 '08 #5
acoder
16,027 Expert Mod 8TB
If you try running the PHP page on its own, does it change when you change the text file?
Sep 12 '08 #6
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.
Sep 12 '08 #7
acoder
16,027 Expert Mod 8TB
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();
Sep 12 '08 #8
This works, thanks so much.
Sep 13 '08 #9
acoder
16,027 Expert Mod 8TB
You're welcome :)
Sep 13 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Andrew Wilkinson | last post by:
Hi, First off I know that in almost all cases this would be a terrible thing to do, but this is an unusual case where this makes sense. Basically I have a procedure where you pass a string...
12
by: Kevin Lyons | last post by:
Hello, I am trying to get my select options (courses) passed correctly from the following URL: http://www.dslextreme.com/users/kevinlyons/selectBoxes.html I am having difficulty getting the...
2
by: Chris | last post by:
I am new to these forums and hope I am in the right place! I am working on an ASP (3.0) page that displays hotel data from a recordset in a table. In the last column of each row, I want to...
136
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their...
16
by: Roman Ziak | last post by:
Hello, there were times when I used to be looking for a way to access JavaScript Global object similar to those found in VBScript or PHP ($GLOBALS). At present this has only academic value for...
1
pbmods
by: pbmods | last post by:
VARIABLE SCOPE IN JAVASCRIPT LEVEL: BEGINNER/INTERMEDIATE (INTERMEDIATE STUFF IN ) PREREQS: VARIABLES First off, what the heck is 'scope' (the kind that doesn't help kill the germs that cause...
15
by: teppic.xxviii | last post by:
Ok, so this is a little script that I've been working on and off for the past year or so, and finally I think it might be ready to be put to the ultimate test: other people! ...
20
by: shapper | last post by:
Hello, How to create a namespace in Javascript containing two methods? And how to access those methods? Thanks, Miguel
84
by: Patient Guy | last post by:
Which is the better approach in working with Javascript? 1. Server side processing: Web server gets form input, runs it into the Javascript module, and PHP collects the output for document prep....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.