Search code examples
phpajaxsetintervalauto-update

Ajax and setInterval() to auto update text file is not updating


I have two text files that are being display on a web page through ajax. I need these two files to update as soon as more text is added to the text file. When I completed this script I tested it on my localhost and all was working properly. Now I have attempted to get it to work on my web host and the text is displaying, but when it is time to update the files nothing updates.
I tried disabling the caching of the ajax response, but the files still do not update.

Here is the code:

<html>
<head>
<script>
$.ajaxSetup ({
cache: false
});

  function UpdateDAU()
    {
    var xmlhttp;
    if (window.XMLHttpRequest)
      {
      xmlhttp=new XMLHttpRequest();
      }
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
           document.getElementById("UpdateD").innerHTML=xmlhttp.responseText.split('\n').join('<br/>';
        }
      }
    xmlhttp.open("GET","../logs/dau.txt",true);
    xmlhttp.send();
    }

    function UpdateFireBox()
    {
    var xmlhttp;
    if (window.XMLHttpRequest)
      {
      xmlhttp=new XMLHttpRequest();
      }
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        document.getElementById("UpdateF").innerHTML=xmlhttp.responseText.split('\n').join('<br/>');
        }
      }
    xmlhttp.open("GET","../logs/firebox.txt",true);
    xmlhttp.send();
    }


    </script>
</head>
<body>
    <script type="text/javascript">
    UpdateDAU();
    </script>
    <div id="UpdateD">No Logs</div>
    <script type="text/javascript">
    setInterval("UpdateDAU", 1000);
    </script>

    <script type="text/javascript">
    UpdateFireBox();
   </script>
<div id="UpdateF">No Logs</div>
    <script>
    setInterval("UpdateFireBox", 1000);
    </script>

</body>
</html>



Is there something that needs to be changed on the server or is this an issue with my code?
What am I doing wrong?


Solution

  • After researching for the past week I finally have the display updating properly. I could not get the files to update from the text file, so I had to echo the files from a php file before the ajax displayed it.
    This is the code that is working on multiple machines:

    <html>
    <head>
    <script>
    $.ajaxSetup ({
    // Disable caching of AJAX responses */
    cache: false
    });
    
      function UpdateDAU()
        {
        var xmlhttp;
        if (window.XMLHttpRequest)
          {
          xmlhttp=new XMLHttpRequest();
          }
        xmlhttp.onreadystatechange=function()
          {
          if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
            document.getElementById("UpdateD").innerHTML=xmlhttp.responseText.split('\n').join('<br/>');
            }
          }
        xmlhttp.open("GET","getdau.php",true);
        xmlhttp.send();
        setTimeout(function() {
        UpdateDAU();       
        }, 1000)
        }
    
        function UpdateFireBox()
        {
        var xmlhttp;
        if (window.XMLHttpRequest)
          {
          xmlhttp=new XMLHttpRequest();
          }
        xmlhttp.onreadystatechange=function()
          {
          if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
            document.getElementById("UpdateF").innerHTML=xmlhttp.responseText.split('\n').join('<br/>');
            }
          }
        xmlhttp.open("GET","getfirebox.php",true);
        xmlhttp.send();
        setTimeout(function() {
        UpdateFireBox();       
        }, 1000)
        }
    
    
        </script>
    </head>
    <body>
        <script type="text/javascript">
        UpdateDAU();
    </script>
    <div id="UpdateD"><p class="text-warning">If there is no page...<br> Then the guards have not started there rounds.</p></div>
    
        <script type="text/javascript">
        UpdateFireBox();
    </script>
    <div id="UpdateF"><p class="text-warning">If there is no page...<br> Then the guards have not started there rounds.</p></div>
    
    </body>
    </html>
    

    Here is the PHP files used to diplay files:

    <?php
    $file = file_get_contents('../logs/dau.txt', true);
    echo $file;
    ?>
    

    and second PHP file is the same:

    <?php
    $file = file_get_contents('../logs/firebox.txt', true);
    echo $file;
    ?>
    

    Using this code the logs inside txt files are updated and displayed every second without refreshing the page.



    If there is a way to use these same steps but with only 1 php file instead of 2 show me and I will delete my answer and select yours.