如果有自动保存功能,可以相对减少一些突发的意外,我们用xajax来实现自动保存草稿
index.php文件
<?php
require_once('../includes/xajax/xajax.inc.php');
require_once('./function.autosave.php');
$xajax=newxajax();
$xajax->registerFunction('autosave');
$xajax->processRequests();
echo$xajax->getJavascript('../includes/xajax');
?>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/>
<linkrel="icon"href="/favicon.ico"type="image/x-icon"/>
<linkrel="shortcuticon"href="/favicon.ico"type="image/x-icon"/>
<title>自动保存草稿xajax0.2.4示例</title>
<metaname="Description"content=""/>
<metacontent=""name="Keywords"/>
<linkrel="stylesheet"rev="stylesheet"href=""type="text/css"media="all"/>
</head>
<body>
<formname="f1"id="f1"method="post"action=""onsubmit="xajax_autosave(xajax.getFormValues('f1'));returnfalse;"enctype="multipart/form-data">
<inputtype="checkbox"name="Draft_AutoSave"id="Draft_AutoSave"value="1"checked="true"/>
<textareaname="content"id="content"rows="10"cols="50">xajax自动保存草稿示例</textarea>
<divid="content_msg">
</div>
<inputtype="submit"name="submit"id="submit"value="提交数据"/>
<scriptlanguage="javascript">
<!--
//自动保存时间间隔
varAutoSaveTime=10000;
//计时器对象
varAutoSaveTimer;
//首先设置一次自动保存状态
SetAutoSave();
//设置自动保存状态函数
functionSetAutoSave(){
//是否自动保存?
if(document.getElementById("Draft_AutoSave").checked==true)
//是,设置计时器
AutoSaveTimer=setInterval("xajax_autosave(xajax.getFormValues('f1'))",AutoSaveTime);
else
//否,清除计时器
clearInterval(AutoSaveTimer);
}
//-->
</script>
</form>
</body>
</html>
function.autosave.php中定义一个函数autosave
functionautosave($form){
$content=$form['content'];
if($content!=''){
$obj=newxajaxResponse();
$obj->addAlert($content);
$fp=fopen('data.txt','a+');
fwrite($fp,$content.'n');
fclose($fp);
$obj->addAssign('content_msg','innerHTML','成功保存');
return$obj;
}
}
xajax自动保存草稿 主要用到两个javascript函数setInterval和clearInterval
是设定多长时间间隔执行行数,clearInterval用于清除时间