Home >Backend Development >PHP Problem >How to implement editable tables with php and ajax
Preface
Tables are a commonly used way to display data on web pages, and sometimes we need to allow users to edit data through tables, so we need to use editable tables. As a server-side scripting language, php can operate on tables, and when used with ajax, it can update data asynchronously without reloading the entire web page. In this article, we will introduce how to implement editable tables using php and ajax.
Implementation steps
First, create a database named "editable_table" in the mysql database, and then create a A data table named "users" is used to store user information. The structure of the table is as follows:
CREATE TABLE `users` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(50) NOT NULL, `email` varchar(50) NOT NULL, `phone` varchar(20) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Create a php file named "table.php" for reading user information from the database, And display it in table form on the web page. The code is as follows:
<?php // 连接数据库 $conn = mysqli_connect('localhost', 'root', 'password', 'editable_table'); if (!$conn) { die('连接数据库失败: ' . mysqli_connect_error()); } // 查询数据库,获取用户信息 $sql = "SELECT * FROM users"; $result = mysqli_query($conn, $sql); // 输出表格 echo "<table><thead><tr><th>ID</th><th>姓名</th><th>邮箱</th><th>电话</th></tr></thead><tbody>"; while ($row = mysqli_fetch_assoc($result)) { echo "<tr><td>" . $row['id'] . "</td><td>" . $row['name'] . "</td><td>" . $row['email'] . "</td><td>" . $row['phone'] . "</td></tr>"; } echo "</tbody></table>"; // 关闭数据库连接 mysqli_close($conn); ?>
Now we can display user information on the web page, but we want users to be able to edit data through the form. In order to achieve this functionality, we need to add some javascript code.
First, we need to add a "contenteditable" attribute to turn the table cell into an editable state. In addition, we also need to add an event listener to send the modified data to the server when the content in the cell is modified.
The code is as follows:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>可编辑表格</title> </head> <body> <div id="table-container"></div> <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script> // 读取数据表并将其展示在网页上 function loadTable() { $.ajax({ url: 'table.php', type: 'GET', success: function(result) { $('#table-container').html(result); } }); } // 点击单元格时,将它设为可编辑状态 $(document).on('click', 'td[contenteditable=false]', function() { $(this).attr('contenteditable', true); $(this).addClass('editable-cell'); $(this).focus(); }); // 当修改单元格中的内容时,将修改的数据发送到服务器端 $(document).on('blur', 'td[contenteditable=true]', function() { var row = $(this).parent(); var id = row.children().eq(0).text(); var name = row.children().eq(1).text(); var email = row.children().eq(2).text(); var phone = row.children().eq(3).text(); $.ajax({ url: 'update_table.php', type: 'POST', data: { id: id, name: name, email: email, phone: phone }, success: function(result) { loadTable(); } }); $(this).attr('contenteditable', false); $(this).removeClass('editable-cell'); }); // 加载数据表 $(document).ready(function() { loadTable(); }); </script> <style> .editable-cell { background-color: #fff2cc; } </style> </body> </html>
Finally, we need to write a php file named "update_table.php" , used to update new data into the database. The code is as follows:
<?php // 连接数据库 $conn = mysqli_connect('localhost', 'root', 'password', 'editable_table'); if (!$conn) { die('连接数据库失败: ' . mysqli_connect_error()); } // 获取POST请求中的参数 $id = $_POST['id']; $name = $_POST['name']; $email = $_POST['email']; $phone = $_POST['phone']; // 更新数据库中的数据 $sql = "UPDATE users SET name='$name', email='$email', phone='$phone' WHERE id='$id'"; $result = mysqli_query($conn, $sql); // 输出结果 if ($result) { echo "修改成功"; } else { echo "修改失败"; } // 关闭数据库连接 mysqli_close($conn); ?>
At this point, we have completed all the steps to implement editable tables with php and ajax. When we refresh the web page, we can realize the related functions of editable tables.
Summary
In this article, we introduced how to use php and ajax to implement editable tables. By using the "contenteditable" attribute and event listeners, we can make table cells editable and upload the modified data to the server for updates via ajax. In this way, users can easily edit and save data through the web page.
The above is the detailed content of How to implement editable tables with php and ajax. For more information, please follow other related articles on the PHP Chinese website!