当前位置: 移动技术网 > IT编程>开发语言>PHP > php+ajax实现文章自动保存的方法

php+ajax实现文章自动保存的方法

2018年07月07日  | 移动技术网IT编程  | 我要评论

本文实例讲述了php+ajax实现文章自动保存的方法。分享给大家供大家参考。具体分析如下:

php+ajax文章自动保存的方法主是要方便用户,提高用户体验,我们就是用ajax把数据保存一个临时数据,像csdn一样,他可以自动保存用户的数据,这样就是掉电,出现意外你编辑的数据都不人被丢失.

这是自动保存草稿的核心的一部分,

autosavetime(sec) 这个函数是用来开始计时的

cleartimeout(autosavetimer);清除定时器

document.getelementbyid('autosavetimebox').innerhtml=sec+"秒";取得页面中的autosavetimebox对像,并向其写入倒计时

复制代码 代码如下:
if(sec>0) {
       autosavetimer = settimeout("autosavetime("+sec+"-1)",1000);  
     //这里就是如果当sec>0的时候,第一秒执行一次autosavetime这个函数,同时会把sec-1的值写入autosavetimebox中
}else {
          var title=document.getelementbyid('title');
          if(title.value!=''){
                                  autosave_post();
          }else{
                 document.getelementbyid('autosavetimebox').innerhtml='不用保存';   
          }
}

这一部分,就是当sec>0的条件不成立,呵呵,就是sec<=0的时候,开始执行保存草稿,首先会判断文章的标题是否为空,如果不会为空,就执行autosave_post()这个函数,否则就写入‘不用保存'.

php代码如下:

复制代码 代码如下:
var useragent = navigator.useragent.tolowercase();
var is_opera  = (useragent.indexof('opera') != -1);
var is_saf    = ((useragent.indexof('applewebkit') != -1) || (navigator.vendor == 'apple computer, inc.'));
var is_webtv  = (useragent.indexof('webtv') != -1);
var is_ie     = ((useragent.indexof('msie') != -1) && (!is_opera) && (!is_saf) && (!is_webtv));
var is_ie4    = ((is_ie) && (useragent.indexof('msie 4.') != -1));
var is_moz    = ((navigator.product == 'gecko') && (!is_saf));
var is_kon    = (useragent.indexof('konqueror') != -1);
var is_ns     = ((useragent.indexof('compatible') == -1) && (useragent.indexof('mozilla') != -1) && (!is_opera) && (!is_webtv) && (!is_saf));
var is_ns4    = ((is_ns) && (parseint(navigator.appversion) == 4));
var is_mac    = (useragent.indexof('mac') != -1);
if ((is_ie & !is_ie4) || is_moz || is_saf || is_opera)
{
    var allowajax=1;
}else{
    var allowajax=0;
}
var xmlhttp = false;
function makesenddata(postdata,url,functionname,httptype) {
 
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,url,'autosave','post');
    }
}
function autosave()
{
    if(xmlhttp.readystate == 4)
    {
        if(xmlhttp.status == 200)
        {
            var autoresponse=xmlhttp.responsetext;
            var automessage=document.getelementbyid('autosavetimebox');
            if(autoresponse.indexof("<autosave_error>")!=-1)
            {
                automessage.innerhtml='您还没有添写信息,不用保存草稿';
                return false;
            }
            if(autoresponse.indexof("<autosave_ok>")!=-1)
            {
                automessage.innerhtml='保存成功,您可以在发生意外的时候载入草稿';
                finddraft();
            }
        }
    }
}
function finddraft()
{
    if(allowajax==1)
    {
        var url="ajax.php?act=loaddraft";
        makesenddata(null,url,'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,url,'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;
}

自动保存的js代码,代码如下:
复制代码 代码如下:
var autosavetimer;
function autosavetime(sec) {
   cleartimeout(autosavetimer);
   document.getelementbyid('autosavetimebox').innerhtml=sec+"秒";
   if(sec>0) {
       autosavetimer = settimeout("autosavetime("+sec+"-1)",1000);
   }else {
       var blogtitle=document.getelementbyid('title');
       if(blogtitle.value!=''){
           autosave_post();
       }else{
           document.getelementbyid('autosavetimebox').innerhtml='不用保存';  
       }
   }
}
function startimer()
{
    var starttime=document.getelementbyid('autosavetimebox').innerhtml;
    if(starttime=='保存成功,您可以在发生意外的时候载入草稿' || starttime=='您还没有添写信息,不用保存草稿')
    {
        starttime='60';
    }else{
        starttime=starttime.replace('秒','');
    }
    var autosavefunbox=document.getelementbyid('autosavefunbox');
    autosavefunbox.innerhtml='<a href="javascript教程:" onclick="javascript:stoptimer()">停止计时</a>';
    starttime==0 ? starttime=60 : starttime=starttime;
    autosavetime(starttime);
}
function stoptimer()
{
    var autosavefunbox=document.getelementbyid('autosavefunbox');
    autosavefunbox.innerhtml='<a href="javascript:" onclick="javascript:startimer()">开始计时</a>';
    cleartimeout(autosavetimer);
}

希望本文所述对大家的php程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网