教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 php实现input输入框失去焦点自动保存输入框的数据

php实现input输入框失去焦点自动保存输入框的数据

发布时间:2016-10-26   编辑:jiaochengji.com
教程集为您提供php实现input输入框失去焦点自动保存输入框的数据等资源,欢迎您收藏本站,我们将为您提供最新的php实现input输入框失去焦点自动保存输入框的数据资源
自动保存常用于编辑器了,要实现数据定时保存我们会使用到ajax功能,下面我们再一起来看一个input输入框失去焦点自动保存输入框的数据实例

最近做一个输入框失去焦点时自动保存数据的功能,当然就是jquery选择器选择input,blur时,ajax提交数据给php文件,php文件保存一下数据咯。主要是要注意一下中文的问题,所以中间需要转一下编码。

下面的实例是一个列表页,有一点类似excel了。

html代码:

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy3241')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy3241>

<table width=”100%” align=”left” border=”0″ cellspacing=”0″ bgcolor=”#CCCCCC”>
<tr onmouseover=”this.className=’on’;” onmouseout=”this.className=”;” align=”center” id=”{$id}” bgcolor=”#FFFFFF”>
<td >{$id}</td>
<td ><input type=”text” id=”title_{$id}” value=”{$title}”  onblur=”ajaxEdit(‘{$id}’,'title’);” ></td>
<td ><textarea id=”description_{$id}”  rows=”4″  onblur=”ajaxEdit(‘{$id}’,'description’);” >{$description}</textarea></td>
<td ><a href=”view.php?aid={$id}” target=”_blank”>预览</a><a href=”edit.php?aid={$id}”>编辑</a></td>
</tr>
</table>

js代码:

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy4648')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy4648>

<script type=”text/javascript”>

function Dd(i) {return document.getElementById(i);}
function ajaxEdit(aid,field){
var value = decodeURI(Dd(field ”_” aid).value);
$.ajax({
type: “POST”,
url: “edit.php”,
data: {dopost:”ajaxSave”,aid:aid,field:field,value:value},
success: function(data){
if(data==”true”){//更新成功
Dd(field ”_” aid).style.border=”#fff”;
}else{//更新失败
alert(“更新失败,可能是网速太慢”);
}
}
});
}
</script>

php代码:(引用了dedecms的函数、方法)

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy4672')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy4672>

if($dopost==’ajaxSave’)
{

$value = urldecode(AutoCharset($value,”UTF-8″,”GB2312″));
//更新主表
$inQuery = “UPDATE `#@__archives` SET $field=’$value’ WHERE id=’$aid’”;
if($dsql->ExecuteNoneQuery($inQuery))
{
echo “true” ;
exit;
}else{
echo “false”;
exit;
}

}

方法二,定时保存草稿功能,防止数据意外丢失!

情况介绍,网站后台编辑器是采用了百度UEditor所见即所得编辑器,我们就是要实现自动定时保存编辑器里的内容。

编辑器的调用方式如下:

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy6673')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy6673>

<script type="text/plain" id="content" name="content" style="width:800px;height:400px;"></script>
<script type="text/javascript">
    var editor = new UE.ui.Editor();
    editor.render('content');
</script>

在发表页面或编辑页的最后加入js调用标签:

<!--自动保存功能 防止数据意外丢失-->

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy8541')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy8541>

<script type="text/javascript" src="localStorag.js"></script>

localStorag.js 的代码如下:

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy9846')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy9846>

/*注意: 本js脚本请在网页源代码最后的地方放置*/
if(!window.localStorage){
alert('您的浏览器不支持 localStorage 技术!');
}else{

var spanObj = document.getElementById('s1');
var saveTimer= setInterval(function(){
var str="";
if(document.all){/*IE*/ str=document.frames[1].document.body.innerHTML; }
else{/*Chrome,ff*/ str=document.getElementById("ueditor_0").contentDocument.body.innerHTML; }
if(str.length>20 && (str.indexOf("。")>-1 || str.indexOf(",")>-1)){ /*有内容才保存 且有句号或逗号*/
localStorage.setItem("ctValue", str);
var d = new Date();
var YMDHMS = d.getFullYear() "-" (d.getMonth() 1) "-" d.getDate() " " d.getHours() ":" d.getMinutes() ":" d.getSeconds();
spanObj.innerText='(数据保存于: ' YMDHMS ')';
setTimeout(function(){ spanObj.innerText=''; },5000);
}
    },25000); //每隔N秒保存一次

function stoplocs(){
clearInterval(saveTimer); //停止保存
//localStorage.removeItem("ctValue"); //清空
}

function showlocs(){
var html = localStorage.getItem("ctValue");
editor.setContent(html);
//alert(localStorage.getItem("ctValue"));
}

}

可以增加停止保存按钮或立即恢复按钮,如下:

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy9347')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy9347>

<a href="javascript:void(0)" onclick="javascript:stoplocs()">停止保存</a>
<a href="javascript:void(0)" onclick="javascript:showlocs()">立即恢复</a>

好了,现在你的网站发表页面/编辑页面就具备自动保存功能了。注意这种方式支持IE8及以上版本的IE,CHROME,Firefox等等大多数主流浏览器。至于IE6,实在是老掉牙了,不在考虑范围内。如果一定要考虑IE6的,请使用上面提供的IE6/ie7兼容方案。

您可能感兴趣的文章:
php实现input输入框失去焦点自动保存输入框的数据
input 输入框获得/失去焦点时隐藏/显示文字(jquery版)
Jquery实现获取焦点、失去焦点的代码
jquery 模拟类搜索框自动完成搜索提示功能(改进)
jquery获取焦点和失去焦点事件代码
jquery input输入框获得与失去焦点时显示、隐藏文字的小例子
文本框获得焦点与失去焦点实现代码
js实现禁用只读文本框获得焦点时的退格键
基于jQuery的input输入框下拉提示层(自动邮箱后缀名)
jQuery 表单验证扩展代码(一)

[关闭]
~ ~