Home > PHP Framework > ThinkPHP > ThinkPHP6 form repeated submission processing: preventing repeated operations

ThinkPHP6 form repeated submission processing: preventing repeated operations

WBOY
Release: 2023-08-12 14:10:50
Original
2021 people have browsed it

ThinkPHP6 form repeated submission processing: preventing repeated operations

ThinkPHP6 form repeated submission processing: preventing repeated operations

In web application development, form submission is a common operation. However, sometimes users may submit forms repeatedly due to network delays or misoperations, which may cause some problems to the system. In order to solve this problem, we can perform repeated form submission processing in the ThinkPHP6 framework to prevent users from repeated operations.

1. Cause Analysis

There are two main reasons for repeated submission of forms:

1. Network delay: When the user clicks the submit button, the form data is sent to the server for processing. However, due to network delays, the server may not respond in time, and the user mistakenly thinks that the operation failed and clicks the submit button again.

2. Misoperation: After submitting the form, the user may click the submit button multiple times because he is not sure whether the operation is successful, causing the form to be submitted multiple times.

2. Methods to prevent repeated submission of forms

1. Form Token verification: Ensure the uniqueness of the form by adding a Token to the form. Each time the user submits the form, the Token generates a unique value and is stored in the Session. After the server receives the form data, it will verify the validity of the Token. If the verification fails, it means that the form is submitted again.

Code example:

Define a method in the controller to generate Token:

namespace appcontroller;

use thinkController;

class Example extends Controller
{
    public function index()
    {
        // 生成Token
        $token = md5(uniqid(rand(), true));
        
        // 保存Token到Session
        session('token', $token);
        
        // 渲染模板,将Token传递给前端
        return $this->fetch('index', ['token' => $token]);
    }
}
Copy after login

Add Token hidden field in the template:

<form action="/example/submit" method="post">
    <input type="hidden" name="token" value="{{ $token }}">
    <!-- 其他表单元素 -->
    <button type="submit">提交</button>
</form>
Copy after login

Verify the validity of the Token in the controller:

namespace appcontroller;

use thinkController;

class Example extends Controller
{
    public function submit()
    {
        // 获取表单提交的Token值
        $token = input('post.token');
        
        // 判断Token是否有效
        if ($token && $token === session('token')) {
            // 执行表单提交操作
            
            // 清除Session中的Token
            session('token', null);
            
            // 返回成功页面
            return '提交成功!';
        } else {
            // 返回错误页面
            return '非法的表单提交!';
        }
    }
}
Copy after login

Through the above code, we can implement Token verification of the form to ensure the uniqueness of the form. When the user clicks the submit button, the server will verify the validity of the token. If the verification fails, it means that the form is submitted again. Otherwise, the normal form submission operation will be performed.

2. Disable repeated submit button: After the user clicks the submit button, disable the button immediately to prevent the user from clicking repeatedly.

Code example:

<script>
    document.getElementById('submitBtn').addEventListener('click', function () {
        // 禁用按钮
        this.disabled = true;
    });
</script>
Copy after login

With the above code, after the user clicks the submit button, the button will be disabled immediately and the user cannot click it repeatedly, thus avoiding repeated submission of the form.

3. Summary

Repeated submission of forms is a common problem that needs to be dealt with in web application development. The method introduced above is a common way to prevent repeated submission of forms. You can choose a suitable method according to the actual situation of the project to handle repeated submission of forms to ensure the stability and security of the system.

The above is the detailed content of ThinkPHP6 form repeated submission processing: preventing repeated operations. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template