php+ajax實(shí)現(xiàn)文章自動保存的方法_PHP教程
推薦:php實(shí)現(xiàn)監(jiān)控varnish緩存服務(wù)器的狀態(tài)這篇文章主要介紹了php實(shí)現(xiàn)監(jiān)控varnish緩存服務(wù)器的狀態(tài),Varnish是一款高性能的開源HTTP加速器,可以替代Squid、Nginx等服務(wù)器,需要的朋友可以參考下 當(dāng)varnish和網(wǎng)站部署在同一臺服務(wù)器上的時候,我們不可能隨時登錄上服務(wù)器去查看varnish的命中率,沒想到有大神早就寫了
這篇文章主要介紹了php+ajax實(shí)現(xiàn)文章自動保存的方法,可實(shí)現(xiàn)文章內(nèi)容的實(shí)時保存,防止突發(fā)情況下的數(shù)據(jù)丟失,是非常實(shí)用的技巧,需要的朋友可以參考下
本文實(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ù)是用來開始計(jì)時的
clearTimeout(autosavetimer);清除定時器
document.getElementById('autosavetimebox').innerHTML=sec+"秒";獲得頁面中的autosavetimebox對像,并向其寫入倒計(jì)時
代碼如下: if(sec>0) {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 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);
// 當(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代碼,代碼如下:
代碼如下: 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=='保存成功,您可以在發(fā)生意外的時候載入草稿' || starttime=='您還沒有添寫信息,不用保存草稿')
{
starttime='60';
}else{
starttime=starttime.replace('秒','');
}
var autosavefunbox=document.getElementById('autosavefunbox');
autosavefunbox.innerHTML='<a href="javascript教程:" onClick="javascript:stoptimer()">停止計(jì)時</a>';
starttime==0 ? starttime=60 : starttime=starttime;
autosavetime(starttime);
}
function stoptimer()
{
var autosavefunbox=document.getElementById('autosavefunbox');
autosavefunbox.innerHTML='<a href="javascript:" onClick="javascript:startimer()">開始計(jì)時</a>';
clearTimeout(autosavetimer);
}
希望本文所述對大家的php程序設(shè)計(jì)有所幫助。
分享:php在線解壓ZIP文件的方法這篇文章主要介紹了php在線解壓ZIP文件的方法,包含了較為完整的上傳與zip解壓功能,非常具有實(shí)用價值,需要的朋友可以參考下 本文實(shí)例講述了php在線解壓ZIP文件的方法。分享給大家供大家參考。具體分析如下: 在PHP的函數(shù)庫中只找到了個ZLIB的函數(shù)還跟壓縮有點(diǎn)關(guān)系,但是使
- php實(shí)現(xiàn)監(jiān)控varnish緩存服務(wù)器的狀態(tài)
- php在線解壓ZIP文件的方法
- php站內(nèi)搜索關(guān)鍵詞變亮的實(shí)現(xiàn)方法
- php使用PDO操作MySQL數(shù)據(jù)庫實(shí)例
- 在php與MySql中計(jì)算時間差的方法解析
- WordPress自定義時間顯示格式
- php實(shí)現(xiàn)插入數(shù)組但不影響原有順序的方法
- 使用新浪微博API的OAuth認(rèn)證發(fā)布微博實(shí)例
- 新浪微博OAuth認(rèn)證和儲存的主要過程詳解
- php簡單實(shí)現(xiàn)無限分類樹形列表的方法
- PHP調(diào)用.NET的WebService 簡單實(shí)例
- PHP輸出日歷表代碼實(shí)例
PHP教程Rss訂閱編程教程搜索
PHP教程推薦
- PHP使用PDO連接Access的方法
- PHP 過濾頁面中的BOM(實(shí)現(xiàn)代碼)
- 解析PHP5在Apache下的兩種模式的安裝
- PHP實(shí)例:上傳多個圖片并校驗(yàn)的代碼
- 將IP地址轉(zhuǎn)換為整型數(shù)字的PHP方法、Asp方法和MsSQL方法、MySQL方法
- 比較簡單的百度網(wǎng)盤文件直鏈PHP代碼
- PHP實(shí)現(xiàn)將視頻轉(zhuǎn)成MP4并獲取視頻預(yù)覽圖的方法
- php實(shí)現(xiàn)以只讀方式打開文件的方法
- php4和php5單態(tài)模式(Singleton Pattern)寫法
- 揭秘40條技巧優(yōu)化php代碼
- 相關(guān)鏈接:
- 教程說明:
PHP教程-php+ajax實(shí)現(xiàn)文章自動保存的方法。