jquery adds table row database
With the continuous development of Internet technology, more and more enterprises, institutions, and individuals have begun to convert their businesses to the Internet. As a window for corporate and personal display and interaction, the website has wide applicability and convenience. In web pages, tables are the most basic way of displaying data and are very important in practical applications. jquery is one of the most widely used JavaScript libraries currently. It provides many convenient methods to easily operate DOM elements, interact with background data through ajax technology, and achieve dynamic updates.
This article will introduce how to use jquery to add table rows and save the data to the database.
1. Preparation
1. Create the database and related tables, and establish the field names and data types.
This article takes Mysql as an example to create a database named test_db, and create a table named test_table in the database. The table contains four fields: id INT(11) AUTO_INCREMENT, name VARCHAR( 20), sex VARCHAR(4), age INT(4), where the id field is automatically incremented.
CREATE DATABASE test_db;
USE test_db;
CREATE TABLE test_table(
id INT(11) AUTO_INCREMENT PRIMARY KEY, name VARCHAR(20) NOT NULL, sex VARCHAR(4) NOT NULL, age INT(4) NOT NULL
);
2.Introduce the jquery library file into the web page
This article uses the jquery3.5.1 version. You can download the file from the official website, or you can use the cdn link to import it directly.
2. Implementation process
1. Page layout
First, you need to create a table in the web page and set the header.
Name | gender | age | operate |
---|
2. Add table rows
Next, you need to implement a function to add table rows. When the "Add" button is clicked, the function will be executed and the new row will be inserted into The last row of the table.
function addRow() {
var name = $("#nameInput").val(); var sex = $("#sexInput").val(); var age = $("#ageInput").val(); var tr = $("<tr></tr>"); var td1 = $("<td>" + name + "</td>"); var td2 = $("<td>" + sex + "</td>"); var td3 = $("<td>" + age + "</td>"); var td4 = $("<td><button onclick="deleteRow(this)">删除</button></td>"); tr.append(td1, td2, td3, td4); $("#testTable tbody").append(tr);
}
In this function, first get the name, gender and age entered by the user, and use jquery’s $() function to create a New row elements, and use the $() function to create four new column elements, used to display name, gender, age and operation. Finally, add the four column elements to the row elements, and add the row element to the last row of the table.
3. Delete table rows
When you need to delete a row, you can execute the following function:
function deleteRow(obj) {
$(obj).parents("tr").remove();
}
This function uses jquery's parents() method to find the row element where the button is located, and uses the remove() method to remove it from the DOM tree.
4. Save data to the database
When the user adds a row of data, the data needs to be saved to the database, which can be achieved through ajax technology. First, you need to write a server-side interface that can accept requests and save data to the database.
header("content-type:text/html;charset=utf-8");
$conn = mysqli_connect("localhost", "root", "123456 ", "test_db");
$name = $_POST['name'];
$sex = $_POST['sex'];
$age = $_POST['age'];
$sql = "INSERT INTO test_table (name, sex, age) VALUES ('$name', '$sex', '$age')";
mysqli_query($conn, $sql);
mysqli_close($conn);
?>
In this interface, first connect to the database, then obtain the data submitted by the user, and finally insert the data into the test_table table based on the obtained data. Finally close the database connection.
In the front-end code, when the user submits data, ajax technology needs to be used to send the data to the server. The code to send data is as follows:
function saveData() {
var name, sex, age; $("#testTable tbody tr").each(function (index) { name = $(this).find("td:eq(0)").text().trim(); sex = $(this).find("td:eq(1)").text().trim(); age = $(this).find("td:eq(2)").text().trim(); $.post("save.php", {name:name, sex:sex, age:age}, function () { alert("保存成功!"); }); });
}
This function is used to traverse the entire table, obtain the data of each row, and use ajax technology to The data is transferred to the save.php file on the server side. After the data is saved successfully on the server side, the front-end will pop up a prompt message "Save successfully!".
3. Summary
This article introduces how to use jquery to add table rows and save the data to the database. In actual applications, the relevant code can be modified appropriately according to business needs to meet your own needs. At the same time, this article also provides a simple idea that can guide readers to understand the application of jquery and ajax technology more deeply.
The above is the detailed content of jquery adds table row database. 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

AI Hentai Generator
Generate AI Hentai for free.

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



The article discusses useEffect in React, a hook for managing side effects like data fetching and DOM manipulation in functional components. It explains usage, common side effects, and cleanup to prevent issues like memory leaks.

The article explains useContext in React, which simplifies state management by avoiding prop drilling. It discusses benefits like centralized state and performance improvements through reduced re-renders.

Article discusses connecting React components to Redux store using connect(), explaining mapStateToProps, mapDispatchToProps, and performance impacts.

Article discusses preventing default behavior in event handlers using preventDefault() method, its benefits like enhanced user experience, and potential issues like accessibility concerns.

The article discusses the advantages and disadvantages of controlled and uncontrolled components in React, focusing on aspects like predictability, performance, and use cases. It advises on factors to consider when choosing between them.

React combines JSX and HTML to improve user experience. 1) JSX embeds HTML to make development more intuitive. 2) The virtual DOM mechanism optimizes performance and reduces DOM operations. 3) Component-based management UI to improve maintainability. 4) State management and event processing enhance interactivity.

Vue 2's reactivity system struggles with direct array index setting, length modification, and object property addition/deletion. Developers can use Vue's mutation methods and Vue.set() to ensure reactivity.

The article discusses defining routes in React Router using the <Route> component, covering props like path, component, render, children, exact, and nested routing.
