Wednesday, February 22, 2012

Display txt file using AJAX technique


This program is for implementing AJAX concept using PHP . It will display a content of Text file in your browser window using AJAX technique. Go through the following steps and get result. 



Step 1) Create a test file called hello.txt and save it in same location with program and put “Hello world” text within text file

Step 2) Copy and paste below code into a HTML file and run in your web server.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="javascript">
function hello()
{
                var xmlhttp;
                xmlhttp=new XMLHttpRequest();

xmlhttp.onreadystatechange=function()
                {
                                if (xmlhttp.readyState==4 && xmlhttp.status==200)
                                {
                                document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
                                }             
                }
xmlhttp.open("GET","hello.txt",true);
xmlhttp.send();
}
</script>
</head>
<body>
<form name="myform">
                <p id="myDiv"></p>
                <input type="button" onClick="hello()" value="Click me">
</form>
</body>
</html>

2 comments:

  1. Great!

    How about updating the text file content thru AJAX?

    ReplyDelete
  2. yes I need this too. what about dynamicly updating text file ?

    ReplyDelete