Ajax implementation Input box lost focus automatically save the input box data example

Source: Internet
Author: User
Tags php file php code

Recently do an input box lost focus automatically save the data function, of course, is the jquery selector selection Input,blur, Ajax submission data to PHP files, php file to save the data slightly. The main thing is to pay attention to the Chinese problem, so the middle need to turn the code.

The following example is a list page, a bit like Excel.

HTML code:

The code is as follows Copy Code

<table width= "100%" align= "left" border= "0″cellspacing=" 0″bgcolor= "#CCCCCC" >
<tr onmouseover= "This.classname= '"; "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" > Preview </a><a href= "edit.php?aid={$id}" > Edit </a></td>
</tr>
</table>

JS Code:

  code is as follows copy code

<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") {//update succeeded
Dd (field+ "_" +aid). style.border= "#fff";
} else{//update failed
Alert ("Update failed, may be too slow");

}
});
}
</script>

PHP code: (a function, method referenced Dedecms)

if ($dopost = = ' Ajaxsave ')
{

$value = UrlDecode (Autocharset ($value, "Utf-8″," Gb2312″));
//Update primary table
$inQuery = "Update ' #@__archives ' SET $field = ' $value ' WHERE id= ' $aid ';
if ($dsql-> Executenonequery ($inQuery))//www.111cn.net
{
echo "true";
Exit;
} Else{
Echo false;
exit;
}

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.