大佬教程收集整理的这篇文章主要介绍了php+ajax实现文章自动保存的方法,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
本文实例讲述了php+ajax实现文章自动保存的方法。分享给大家供大家参考。具体分析如下:
php+ajax文章自动保存的方法主是要方便用户,提高用户体验,我们就是用ajax把数据保存一个临时数据,像csdn一样,他可以自动保存用户的数据,这样就是掉电,出现意外你编辑的数据都不人被丢失.
这是自动保存草稿的核心的一部分,
autosavetime(seC) 这个函数是用来开始计时的
clearTimeout(autosavetimer);清除定时器
document.getElementById('autosavetimebox').innerHTML=sec+"秒";取得页面中的autosavetimebox对像,并向其写入倒计时
php代码如下:
var posturl=url;
try {
xmlhttp = new ActiveXObject("Msxml2.XMLhttp");
} catch (E) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLhttp");
} catch (e2) {
xmlhttp = false;
}
}
if (!xmlhttp && typeof XMLhttprequest != 'undefined') {
xmlhttp = new XMLhttprequest();
}
if (!xmlhttp) {
alert('CAnnot send an XMLhttp request');
return false;
}
// 提交表单的方式
xmlhttp.open(httptype,posturl,truE);
// 当表单提交完成后触发一个事件
var changefunc="xmlhttp.onreadystatechange = "+functionName; ///////from bob
eval (changefunc);
xmlhttp.setrequestHeader('Content-Type','application/x-www-form-urlencoded');
xmlhttp.send(postData);
}
function autosave_post()
{
var title=document.getElementById('title').value;
var content = window.frames["Editor"].window.frames["HtmlEditor"].document.getElementsByTagName("BODY")[0].innerHTML;
var postTime=document.getElementById('postTime').value;
if(allowajax==1)
{
content=postencode(content);
title=postencode(titlE);
var post="title="+title+"&content="+content+"&postTime="+postTime+"";
var url="ajax.php?act=autosave";
makeSendData(post,'autosave','POST');
}
}
function autosave()
{
if(xmlhttp.readyState == 4)
{
if(xmlhttp.status == 200)
{
var autoresponse=xmlhttp.responseText;
var automessage=document.getElementById('autosavetimebox');
if(autoresponse.indexOf("
{
automessage.innerHTML='您还没有添写信息,不用保存草稿';
return false;
}
if(autoresponse.indexOf("
{
automessage.innerHTML='保存成功,您可以在发生意外的时候载入草稿';
finddraft();
}
}
}
}
function finddraft()
{
if(allowajax==1)
{
var url="ajax.php?act=loaddraft";
makeSendData(null,'loaddraft','POST');
}
}
function loaddraft()
{
var draftbox=document.getElementById('draft');
if(xmlHttp.readyState < 4)
{
draftbox.innerHTML='草稿载入中...';
}
if(xmlHttp.readyState == 4)
{
if(xmlHttp.status == 200)
{
draftbox.innerHTML=xmlHttp.responseText;
}
}
}
function cleardraft()
{
if(allowajax==1)
{
var url="ajax.php?act=cleardraft";
makeSendData(null,'nodraft','POST');
}
}
function nodraft()
{
var draftbox=document.getElementById('draft');
if(xmlHttp.readyState < 4)
{
draftbox.innerHTML='载入中...';
}
if(xmlHttp.readyState == 4)
{
if(xmlHttp.status == 200)
{
draftbox.innerHTML=xmlHttp.responseText;
}
}
}
//encode string
function postencode (str) {
str=encodeURIComponent(str);
if (is_moz) str=str.replace(/%0A/g,"%0D%0A"); //from bob
return str;
}
希望本文所述对大家的php程序设计有所帮助。
以上是大佬教程为你收集整理的php+ajax实现文章自动保存的方法全部内容,希望文章能够帮你解决php+ajax实现文章自动保存的方法所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。