<Script language = "JavaScript">
// Create Ajax
Function initxmlhttp ()
{
VaR XMLHTTP
Try {
XMLHTTP = new activexobject ("msxml2.xmlhttp ");
} Catch (e ){
Try {
XMLHTTP = new activexobject ("Microsoft. XMLHTTP ");
} Catch (e ){
XMLHTTP = false;
}
}
If (! XMLHTTP & typeof XMLHttpRequest! = 'Undefined '){
Try {
XMLHTTP = new XMLHttpRequest ();
} Catch (e ){
XMLHTTP = false;
}
}
If (! XMLHTTP & window. createrequest ){
Try {
XMLHTTP = Window. createrequest ();
} Catch (e ){
XMLHTTP = false;
}
}
Return XMLHTTP;
}
// Read and display a file from a text file
Function readtext ()
{
VaR readbutton = Document. getelementbyid ('read ');
VaR showcontent = Document. getelementbyid ('content ');
Readbutton. Disabled = "disabled ";
Showcontent. innerhtml = 'reading, loading .....';
VaR XMLHTTP = initxmlhttp ();
VaR url = "operation. php? Action = read ";
XMLHTTP. Open ("get", URL, true );
XMLHTTP. onreadystatechange = function (){
If (XMLHTTP. readystate = 4 & XMLHTTP. Status = 200)
{
Showcontent. innerhtml = XMLHTTP. responsetext;
Readbutton. Disabled = "";
Document. getelementbyid ('edit'). Disabled = "";
}
}
XMLHTTP. Send (null );
}
// Switch to edit mode
Function edit ()
{
Document. getelementbyid ('edit'). Disabled = "disabled ";
VaR STR = '';
STR + = '<textarea name = "textcontent" Cols = "50" rows = "8" id = "textcontent"> ';
STR + = '</textarea> ';
Document. getelementbyid ('content'). innerhtml = STR;
Document. getelementbyid ('update'). Disabled = "";
}
// Update data to a text file
Function Update ()
{
VaR XMLHTTP = initxmlhttp ();
VaR url = "operation. php? Action = write ";
VaR DATA = "content =" + document. All. textcontent. value;
XMLHTTP. Open ("Post", URL, true );
XMLHTTP. setRequestHeader ("Content-Type", "application/X-WWW-form-urlencoded ");
XMLHTTP. Send (data );
XMLHTTP. onreadystatechange = function (){
If (XMLHTTP. readystate = 4 & XMLHTTP. Status = 200)
{
Document. getelementbyid ('content'). innerhtml = XMLHTTP. responsetext;
Document. getelementbyid ('update'). Disabled = "disabled ";
}
}
}
</SCRIPT>
<Body>
<Div class = "Main-box">
<Div class = "head-Dark-box"> Ajax demo -- operate a text file </div>
<Div class = "body-box">
<Div class = "Tip-MSG"> A simple Ajax demo. Main functions: <br/>
1. Read and display text files. <Br/>
2. Update a text file.
</Div>
<Div class = "alt-table"> <br/>
<Input id = "read" name = "read" type = "button" class = "button" value = "read" onclick = "readtext ()"/>
<Input id = "edit" Disabled = "disabled" name = "edit" type = "button" class = "button" value = "edit" onclick = "Edit () "/>
<Input id = "Update" Disabled = "disabled" name = "Update" type = "button" class = "button" value = "Update" onclick = "Update () "/>
<Div id = "content" class = "textbox-title"> </div>
</Div>
</Div>
<Div id = "Copyright" class = "foot-SQL"> copyright @ 2006 <a href = "http://www.phpobject.net"> www.phpobject.net </a>! </Div>
</Div>
</Body>
The following is ASP code:
<%
Set FSO = server. Createobject ("scripting. FileSystemObject ")
If request. querystring ("action") = "read" then
Set txtfile = FSO. opentextfile (server. mappath ("demo.txt "))
Response. Write "Response. Write "<HR width = '000000' color = '# cc9999'>"
Response. Write "<PRE>"
While not txtfile. atendofstream
Response. Write txtfile. Readline & "<br>"
Wend
Response. Write "<PRE>"
Else
Set txtfile = FSO. opentextfile (server. mappath ("demo.txt"), 2, true)
Txtfile. writeline request ("content ")
End if
Txtfile. Close
%>