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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mrcheeky
    New Member
    • Sep 2008
    • 7

    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:

    Code:
    graph_counter1 81
    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
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Give your script tag an ID and set its src to the PHP file:
    [code=javascript]document.getEle mentById("scrip tID").src = "realtime_read_ values.php?grap h_request=graph _counter1";[/code]

    Comment

    • mrcheeky
      New Member
      • Sep 2008
      • 7

      #3
      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:

      Code:
      <html>
      <head>
      <script type="text/javascript" id='scriptID'></script>
      <script type="text/javascript">  
      function AddBar1()
      {
      document.getElementById("scriptID").src = "realtime_read_values.php?graph_request=graph_counter1";
      document.write(random_number);
      setTimeout('AddBar1()', 5000);
      }
      </script>
      </head>
      <body onload="AddBar1()">
      </body>
      </html>

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        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:
        [code=javascript]var txt = document.create TextNode(" some text ");
        document.body.a ppendChild(txt) ;[/code]

        Comment

        • mrcheeky
          New Member
          • Sep 2008
          • 7

          #5
          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:

          Code:
          graph_counter1 81
          to

          Code:
          graph_counter1 82
          it doesn't update on my html page. Any thoughts?

          Thanks again for helping me.

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

          Code:
          <html>
          <head>
          <script type="text/javascript" id="scriptID" src="realtime_read_values.php?graph_request=graph_counter1"></script>
          <script type="text/javascript">  
          function AddBar1()
          {
          document.getElementById("scriptID").src = "realtime_read_values.php?graph_request=graph_counter1";
          var txt = document.createTextNode(random_number);
          document.body.appendChild(txt);
          setTimeout('AddBar1()', 5000);
          }
          </script>
          </head>
          <body onload="AddBar1()">
          </body>
          </html>

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            If you try running the PHP page on its own, does it change when you change the text file?

            Comment

            • mrcheeky
              New Member
              • Sep 2008
              • 7

              #7
              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.

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                It may be caching the page. Try:
                [code=javascript]document.getEle mentById("scrip tID").src = "realtime_read_ values.php?grap h_request=graph _counter1&d=" + (new Date()).getTime ();[/code]

                Comment

                • mrcheeky
                  New Member
                  • Sep 2008
                  • 7

                  #9
                  This works, thanks so much.

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #10
                    You're welcome :)

                    Comment

                    Working...