Ajax demo -- operate a text file

Source: Internet
Author: User

<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
%>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.