php+ajax實(shí)現(xiàn)文章自動保存的方法
本文實(shí)例講述了php+ajax實(shí)現(xiàn)文章自動保存的方法。分享給大家供大家參考。具體分析如下:
php+ajax文章自動保存的方法主是要方便用戶,提高用戶體驗(yàn),我們就是用ajax把數(shù)據(jù)保存一個臨時數(shù)據(jù),像csdn一樣,他可以自動保存用戶的數(shù)據(jù),這樣就是掉電,出現(xiàn)意外你編輯的數(shù)據(jù)都不人被丟失.
這是自動保存草稿的核心的一部分,
autosavetime(sec) 這個函數(shù)是用來開始計時的
clearTimeout(autosavetimer);清除定時器
document.getElementById('autosavetimebox').innerHTML=sec+"秒";取得頁面中的autosavetimebox對像,并向其寫入倒計時
autosavetimer = setTimeout("autosavetime("+sec+"-1)",1000);
//這里就是如果當(dāng)sec>0的時候,第一秒執(zhí)行一次autosavetime這個函數(shù),同時會把sec-1的值寫入autosavetimebox中
}else {
var title=document.getElementById('title');
if(title.value!=''){
autosave_post();
}else{
document.getElementById('autosavetimebox').innerHTML='不用保存';
}
}
這一部分,就是當(dāng)sec>0的條件不成立,呵呵,就是sec<=0的時候,開始執(zhí)行保存草稿,首先會判斷文章的標(biāo)題是否為空,如果不會為空,就執(zhí)行autosave_post()這個函數(shù),否則就寫入‘不用保存'.
php代碼如下:
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);
// 當(dāng)表單提交完成后觸發(fā)一個事件
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='保存成功,您可以在發(fā)生意外的時候載入草稿';
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代碼,代碼如下:
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=='保存成功,您可以在發(fā)生意外的時候載入草稿' || 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程序設(shè)計有所幫助。
- 使用PHP+AJAX讓W(xué)ordPress動態(tài)加載文章的教程
- php+ajax+jquery實(shí)現(xiàn)點(diǎn)擊加載更多內(nèi)容
- php+mysql結(jié)合Ajax實(shí)現(xiàn)點(diǎn)贊功能完整實(shí)例
- php AJAX POST的使用實(shí)例代碼
- php基于jquery的ajax技術(shù)傳遞json數(shù)據(jù)簡單實(shí)例
- PHP+ajax 無刷新刪除數(shù)據(jù)
- php+ajax實(shí)現(xiàn)無刷新分頁的方法
- PHP中運(yùn)用jQuery的Ajax跨域調(diào)用實(shí)現(xiàn)代碼
- PHP Ajax實(shí)現(xiàn)頁面無刷新發(fā)表評論
- PHP+Ajax實(shí)現(xiàn)的博客文章添加類別功能示例
相關(guān)文章
php中通過DirectoryIterator刪除整個目錄的方法
這篇文章主要介紹了php中通過DirectoryIterator刪除整個目錄的方法,實(shí)例分析了php通過DirectoryIterator類操作目錄的技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-03-03php Undefined index和Undefined variable的解決方法
這段時間在做項(xiàng)目過程中老是出現(xiàn)這個提示,起初是用$act來接受表單post過來的數(shù)據(jù)2008-03-03PHP實(shí)現(xiàn)基于3DES算法加密解密字符串示例
這篇文章主要介紹了PHP實(shí)現(xiàn)基于3DES算法加密解密字符串,簡單描述了3DES加密算法的概念、原理并結(jié)合實(shí)例形式分析了3DES加密算法具體定義與使用技巧,需要的朋友可以參考下2018-08-08php中經(jīng)典方法實(shí)現(xiàn)判斷多維數(shù)組是否為空
傳統(tǒng)的判斷數(shù)組為空 一般用count或者key 而且多維數(shù)組的話很麻煩2011-10-10php實(shí)現(xiàn)不通過擴(kuò)展名準(zhǔn)確判斷文件類型的方法【finfo_file方法與二進(jìn)制流】
這篇文章主要介紹了php實(shí)現(xiàn)不通過擴(kuò)展名準(zhǔn)確判斷文件類型的方法,涉及php中finfo_file方法與二進(jìn)制流針對文件類型的相關(guān)操作技巧,需要的朋友可以參考下2017-04-04php使用Header函數(shù),PHP_AUTH_PW和PHP_AUTH_USER做用戶驗(yàn)證
這篇文章主要介紹了php使用Header函數(shù),PHP_AUTH_PW和PHP_AUTH_USER做用戶驗(yàn)證的方法,結(jié)合實(shí)例形式分析了PHP使用Header函數(shù)調(diào)用登錄驗(yàn)證及PHP_AUTH_PW和PHP_AUTH_USER進(jìn)行驗(yàn)證處理的相關(guān)技巧,需要的朋友可以參考下2016-05-05完美解決PHP中的Cannot modify header information 問題
以下是對PHP中的Cannot modify header information問題的解決方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過來參考下2013-08-08PHP與MongoDB簡介|安全|M+PHP應(yīng)用實(shí)例詳解
本篇文章是對PHP中的MongoDB簡介|安全|M+PHP應(yīng)用實(shí)例進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06