<script language="javascript">
//建立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;
}
//從文字檔中讀取檔案並且顯示
function readText()
{
var readButton=document.getElementById('read');
var showcontent=document.getElementById('content');
readButton.disabled="disabled";
showcontent.innerHTml='正在讀取,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);
}
//轉換到編輯模式
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="";
}
//更新資料到文字檔
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——操作文字檔</div>
<div class="body-box">
<div class="tip-msg">一個簡單的AJAX示範。主要功能有 :<br />
1、讀取文字檔並顯示。<br />
2、更新文字檔。
</div>
<div class="alt-table" ><br />
<input id="read" name="read" type="button" class="button" value="讀取" onclick="readText()" />
<input id="edit" disabled="disabled" name="edit" type="button" class="button" value="編輯" onclick="edit()" />
<input id="update" disabled="disabled" name="update" type="button" class="button" value="更新" 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>
以下是asp code:
<%
set fso=server.CreateObject("Scripting.FileSystemObject")
if Request.QueryString("action")="read" then
Set txtFile=fso.OpenTextFile(Server.MapPath("demo.txt"))
Response.Write "<h3>text.txt content is following:</h3>"
Response.Write "<hr width='100%' 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
%>