jQuery+PHP implements the method of editing tables and saving them
This time I will bring you the jQuery PHP method of editing a table and saving it. What are the precautions for the jQuery PHP method of editing a table and saving it? The following is a practical case, let’s take a look.
In this example, we will use jQuery to turn a text information into an editable form with one click. You can edit the text content and then click the "OK" button, and the new content will be sent to The background PHP program processes and saves it to the database; when the "Cancel" button is clicked, the page returns to the initial state. Friends in need can refer to the following Applicable scenarios of this example: When viewing detailed information, such as user details , if you find that some of the field information needs to be modified, you can directly click on the field content to modify it, saving the user time. (The traditional method is to enter an editing page and list all the edited field information, even if you only need to edit one of them. Two fields of content, and then click Submit) improves the WEB response speed, thus improving the front-end user experience.This example relies on the jquery library and is based on plug-ins. It has the following characteristics: Real-time editing, real-time response in the background, and instant partial refresh .
The input form type can be customized. Currently jeditable provides text, select, and textarea types.
Respond to the Enter and ESC keys of the keyboard.
Plug-in mechanism, this example provides integration with the datepicker calendar control of jquery ui.
Let’s explain the implementation process step by step.
XHTMLWe need to make a form, as follows:
<table width="100%" border="0" cellspacing="0" cellpadding="0"> <thead> <tr class="table_title"> <td colspan="4"><span class="open"></span>客户信息</td> </tr> </thead> <tbody> <tr> <td width="20%" class="table_label">姓名</td> <td width="30%" class="edit" id="username">李小三</td> <td width="20%" class="table_label">办公电话</td> <td width="30%" class="edit" id="phone">021-12345678</td> </tr> <tr> <td class="table_label">称谓</td> <td class="edit" id="solutation">先生</td> <td class="table_label">手机</td> <td class="edit" id="mobile">13800138000</td> </tr> <tr> <td class="table_label">公司名称</td> <td class="edit" id="company">常丰集团</td> <td class="table_label">电子邮箱</td> <td class="edit" id="email">lrfbeyond@163.com</td> </tr> <tr> <td class="table_label">潜在客户来源</td> <td class="edit_select" id="source">公共关系</td> <td class="table_label">有限期</td> <td class="datepicker" id="sdate">2011-11-30</td> </tr> <tr> <td class="table_label">职位</td> <td class="edit" id="job">部门经理</td> <td class="table_label">网站</td> <td class="edit" id="web">www.helloweba.com</td> </tr> <tr> <td class="table_label">创建时间</td> <td>2010-11-04 21:11:59</td> <td class="table_label">修改时间</td> <td id="modifiedtime">2010-11-05 09:42:52</td> </tr> <tr> <td class="table_label">备注</td> <td class="textarea" id="note" colspan="3">备注信息</td> </tr> </tbody> </table>
CSS
table{width:96%; margin:20px auto; border-collapse:collapse;} table td{line-height:26px; padding:2px; padding-left:8px; border:1px solid #b6d6e6;} .table_title{height:26px; line-height:26px; background:url(btn_bg.gif) repeat-x bottom; font-weight:bold; text-indent:.3em; outline:0;} .table_label{background:#e8f5fe; text-align:right; }
jQueryWhen it comes to jquery, you must remember to quote jquery and the jeditable plug-in between the
<script type="text/javascript" src="js/jquery.js"></script> <script type="text/javascript" src="js/jquery.jeditable.js"></script>
$(function(){ $('.edit').editable('save.php', { width :120, height :18, //onblur : 'ignore', cancel : '取消', submit : '确定', indicator : '<img src="loader.gif">', tooltip : '单击可以编辑...' }); });
properties and method calls. You can set the width, height, text information of the button, loading image when submitting, prompt information when the mouse slides over, etc. save.php is the address of the background program where the edited information is finally submitted. Now see if the information in the table can be edited. Jeditable also provides select, textarea type editing, and provides a plug-in API interface.
Let’s look at the processing of the drop-down selection box select:
$('.edit_select').editable('save.php', { loadurl : 'json.php', type : "select", });
$array['老客户'] = '老客户'; $array['独自开发'] = '独自开发'; $array['合作伙伴'] = '合作伙伴'; $array['公共关系'] = '公共关系'; $array['展览会'] = '展览会'; print json_encode($array);
$('.edit_select').editable('save.php', { data : " {'老客户':'老客户','独自开发':'独自开发','合作伙伴':'合作伙伴', '展览会':'展览会'}", type : "select", });
The textarea type is no longer the majority, just change the type type to textarea. PS: The default type is text.
When dealing with date types, I connected a jquery ui calendar plug-in. Of course, don’t forget to introduce the juqery ui plug-in and style:
<link rel="stylesheet" type="text/css" href="css/jquery-ui.css" /> <script type="text/javascript" src="js/jquery-ui.js"></script>
The code called by
$.editable.addInputType('datepicker', { element : function(settings, original) { var input = $('<input class="input" />'); input.attr("readonly","readonly"); $(this).append(input); return(input); }, plugin : function(settings, original) { var form = this; $("input",this).datepicker(); } });
$(".datepicker").editable('save.php', { width : 120, type : 'datepicker', onblur : "ignore", });
PHPThe edited field information will be sent to the background program save.php program for processing. The work that save.php needs to complete is: receive the field information data submitted by the front end, perform necessary filtering and verification, then update the corresponding field content in the data table, and return the results.
include_once("connect.php"); //连接数据库 $field=$_POST['id']; //获取前端提交的字段名 $val=$_POST['value']; //获取前端提交的字段对应的内容 $val = htmlspecialchars($val, ENT_QUOTES); //过滤处理内容 $time=date("Y-m-d H:i:s"); //获取系统当前时间 if(emptyempty($val)){ echo "不能为空"; }else{ //更新字段信息 $query=mysql_query("update customer set $field='$val',modifiedtime='$time' where id=1"); if($query){ echo $val; }else{ echo "数据出错"; } }
In this way, the editable table is finished. But it is not finished yet. I will attach subsequent articles on the verification of the validity of the input information, so stay tuned.
JQuery operation Table method summary
jquery dynamic operation table row
jQuery to html table Summary of dynamically adding row methods
The above is the detailed content of jQuery+PHP implements the method of editing tables and saving them. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
