ajax自动保存草稿

发布时间:2008-02-29 14:54:00 阅读:1022次

自动保存草稿功能,很好很实用.在网易博客中用到了.自己也写了个.

writestory.asp

<!--#include file="CheckPower.asp"-->
<link rel="stylesheet" href="css/webstylein.css" />
<script src="js/js.js"></script>
<script src="js/prototype.js"></script>
<!--#include file="checkpower.asp"-->
<body onload=window.setTimeout("AddDraft($('title').value,$('author').value,$('storycontent').value,$('objectdesc').value,$('storyid').value,$('picurl').value)",30000)>

<fieldset>
<legend>
撰写故事
</legend>
<label>
<script>
function check(){
if(document.form1.title.value==""){
alert("文章标题不能为空")
document.form1.title.focus();
return false;
}
if(document.form1.author.value==""){
alert("作者不能为空")
document.form1.author.focus();
return false;
}
if(document.form1.storycontent.value==""){
alert("故事内容不能为空")
document.form1.storycontent.focus();
return false;
}
if(document.form1.objectdesc.value==""){
alert("对象描述不能为空")
document.form1.objectdesc.focus();
return false;
}
return true;
}
</script>
<form name="form1" action="addstory.asp" method="post" onSubmit="javascript:return check()">
<table width="80%" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td width="22%" height="24" align="left">文章标题</td>
    <td width="78%"><input name="title" type="text" id="title" class="input_box" /></td>
  </tr>
  <tr>
    <td height="24" align="left">作者</td>
    <td height="24"><input name="author" type="text" id="author" class="input_box" /></td>
  </tr>
  <tr>
    <td height="24" align="left">上传图片</td>
    <td height="24">
 <input name="picurl" type="text" id="picurl"  size="30" class="input_box" />
            <input type="button" name="Submit11" class="input_box" value="upload" onClick="window.open('upfile.asp?formname=form1&amp;editname=picurl&amp;uppath=storyimages&amp;filelx=jpg','','status=no,scrollbars=no,top=20,left=110,width=420,height=165')" />
 </td>
  </tr>
  <tr>
    <td height="24" align="left" valign="top">故事内容</td>
    <td height="24"><textarea name="storycontent" cols="40" rows="10" id="storycontent" class="input_box"></textarea></td>
  </tr>
  <tr>
    <td height="24" align="left" valign="top">推荐对象描述</td>
    <td height="24"><textarea name="objectdesc" cols="40" rows="10" id="objectdesc" class="input_box"></textarea>
 <input type="hidden" name="storyid" id="storyid" />
 </td>
  </tr>
  <tr>
    <td height="34" colspan="2" align="center">
 <input type="submit" name="submit" value="发表故事" class="input_box" /> 
      <input id="AddDraft" type="button" name="Submit2" value="保存至草稿箱" class="input_box" onClick="SaveToDraft($('title').value,$('author').value,$('storycontent').value,$('objectdesc').value,$('storyid').value,$('picurl').value)" /> 
 </td>
  </tr>
    <tr>
    <td height="34" colspan="2" align="center">
   <span id="span1"></span>
   </td>
  </tr>
</table>
</form>
</label>
</fieldset>

 

js.js

function createXMLHttpRequest() {
   if (window.XMLHttpRequest) {//Mozilla
  xmlHttp = new XMLHttpRequest();
   }
  else if (window.ActiveXObject) {//IE
  try {
  xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); 
  } catch (e) {
  try {
  xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); 
  } catch (e) {}
  }
  }
}


function AddDraft(title,author,storycontent,objectdesc,id,picurl){
    createXMLHttpRequest();
 xmlHttp.open("get","AddDraft.asp?id="+id+"&picurl="+picurl+"&title="+title+"&author="+author+"&storycontent="+escape(storycontent)+"&objectdesc="+escape(objectdesc)+"&fresh="+Math.random(),true);
 xmlHttp.onreadystatechange = function(){
 if(xmlHttp.readyState==4){
 document.getElementById("storyid").value = xmlHttp.responseText;
 document.getElementById("span1").innerHTML=""
 }
 else{
 document.getElementById("storyid").value = "";
 document.getElementById("span1").innerHTML="<span style='color:#FF0000'>正在保存草稿中...</span>"
    }
 }
 xmlHttp.send(null); 
window.setTimeout("AddDraft($('title').value,$('author').value,$('storycontent').value,$('objectdesc').value,$('storyid').value,$('picurl').value)",30000);
}

adddraft.asp

<!--#include file="CheckPower.asp"-->
<!--#include file="conn.asp"-->
<!--#include file="closeconn.asp"-->
<%
if request("id")="" then
set rs=server.CreateObject("Adodb.recordset")
rs.open "Select *from story where 1<>2",cn,1,2
rs.addnew
rs("userid")=session("userid")
rs("title")=trim(request("title"))
rs("author")=trim(request("author"))
rs("story")=trim(request("storycontent"))
rs("description")=trim(request("objectdesc"))
rs("isDraft")=true
rs("pic1")=trim(request("picurl"))
storyid=rs("id")
rs.update
response.write storyid
else
set rs=server.CreateObject("Adodb.recordset")
rs.open "Select *from story where 1<>2 and id="&request("id")&"",cn,1,2
rs("userid")=session("userid")
rs("title")=trim(request("title"))
rs("author")=trim(request("author"))
rs("story")=trim(request("storycontent"))
rs("description")=trim(request("objectdesc"))
rs("isDraft")=true
rs("pic1")=trim(request("picurl"))
storyid=rs("id")
rs.update
response.write storyid
end if
call closeconn
%>

如有问题,可以QQ搜索群1028468525加入群聊,欢迎一起研究技术

支付宝 微信

有疑问联系站长,请联系QQ:QQ咨询
上一篇:SQL命令小全
下一篇:ajax跳转

转载请注明:ajax自动保存草稿 出自老鄢博客 | 欢迎分享