Search code examples
javascriptajaxsyslog

How to dynamically update text on a website from a file


I'm a server-side programmer with only basic web experience so I don't know how to accomplish the following task.

I am reading several files simultaneously across a network and merging all the data into a single file. Typically these will be log files but they may be some other sort of text file. Some fancier indexing, searching, sysloggy stuff is going on but that's beside the point.

I'd like to write a basic web interface which will allow the user to view this concatenated file in real-time. I know this will involve some ajaxy javascript dhtml type of stuff, I just dont know exactly what that is and I dont know where to start looking for information other than 'web programming basics' and I'm a little further along than that. Thanks!


Solution

  • You can use ajax to fetch data from either xml/text file.

    If you have any content stored in a text file the following code will work

    Example code

    <script type="text/javascript">
    function loadXMLDoc()
    {
    var xmlhttp;
    if (window.XMLHttpRequest){
      xmlhttp=new XMLHttpRequest();
    }else{
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        document.getElementById("myDiv").innerHTML= xmlhttp.responseText; 
        }
      }
    xmlhttp.open("GET","ajax_info.txt",true);
    xmlhttp.send();
    }
    </script>
    
    <div id="myDiv">Let AJAX change this text</div>
    <button type="button" onclick="loadXMLDoc()">Change Content</button>
    

    View the live example at http://www.w3schools.com/Ajax/tryit.asp?filename=tryajax_first

    View the sample for getting xml data using ajax, http://www.w3schools.com/Ajax/ajax_xmlfile.asp

    You can further read about ajax from here