How to use Layui to develop a personal information management system that supports editability
Overview:
Personal information management system is a very common application scenario, which can Help users manage their personal information, including basic information, contact information, education experience, work experience, etc. This article will introduce how to use Layui to develop a personal information management system that supports editability, providing users with a convenient and fast information management experience.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>个人信息管理系统</title> <!-- 引入Layui的样式文件 --> <link rel="stylesheet" href="path/to/layui/css/layui.css"> </head> <body> <div class="layui-container"> <h1>个人信息</h1> <form class="layui-form"> <div class="layui-form-item"> <label class="layui-form-label">姓名</label> <div class="layui-input-inline"> <input type="text" name="name" lay-verify="required" autocomplete="off" class="layui-input" value="张三"> </div> </div> <div class="layui-form-item"> <label class="layui-form-label">年龄</label> <div class="layui-input-inline"> <input type="text" name="age" lay-verify="required|number" autocomplete="off" class="layui-input" value="20"> </div> </div> <!-- 其他个人信息字段 --> <div class="layui-form-item"> <div class="layui-input-block"> <button class="layui-btn layui-btn-primary" lay-submit lay-filter="save">保存</button> </div> </div> </form> </div> <!-- 引入Layui的JavaScript文件 --> <script src="path/to/layui/layui.js"></script> <script> // 初始化Layui表单组件 layui.use('form', function(){ var form = layui.form; // 监听表单提交事件 form.on('submit(save)', function(data){ // 提交表单数据到后台进行保存 console.log(data.field); // ... return false; // 阻止表单默认提交行为 }); }); </script> </body> </html>
In the above code, we use Layui's form component to implement the form submission function. The form contains two fields: name and age. You can add other personal information fields according to your needs and set corresponding verification rules.
const express = require('express'); const app = express(); // 接收POST请求体的中间件 app.use(express.json()); app.use(express.urlencoded({ extended: true })); app.post('/save', function(req, res) { console.log(req.body); // 输出表单提交的数据 // TODO: 将表单数据保存到数据库或其他持久化方式 res.json({ message: '保存成功' }); }); app.listen(3000, function() { console.log('服务器已启动,监听端口3000'); });
In the above code, we use express.json() and express.urlencoded() middleware to Parse the data in the POST request body. We created a /save route to receive form data and output it to the console, where you can add corresponding business logic.
node app.js
Then, enter http://localhost:3000 in the browser to access the personal information management system page.
Summary:
Through the above steps, we successfully used Layui to develop a personal information management system that supports editability. Users can edit form data in the browser and save the data to the background via the submit button. You can expand and improve the system according to actual needs, such as adding data verification, data persistence and other functions. Layui provides a wealth of components and tools, providing developers with the ability to quickly build front-end interfaces. I hope this article can provide some help to developers when using Layui to develop personal information management systems.
The above is the detailed content of How to use Layui to develop a personal information management system that supports editability. For more information, please follow other related articles on the PHP Chinese website!