How does javascript-JS or PHP end prevent repeated submission?

Source: Internet
Author: User
If you submit the data repeatedly, the data will be accumulated in the data table. Is there a solution? If you submit the data repeatedly, the data will be accumulated in the data table. Is there a solution?

Reply content:

If you submit the data repeatedly, the data will be accumulated in the data table. Is there a solution?

Post/Redirect/GetPRG is a Web design that prevents repeated data submission.

Server:
1. Add a form token to verify that the token passed is invalid.
2. If a field must be unique, set a unique field in the database.
3. The session stores the last part of the submitted data and determines whether the data is consistent.
4. Go to the new page after the operation is successful.
JS end:
1. After submission, cancel the submission event or set it to not clickable.
2. After the operation is successful, reload the page.

Set the field to UNIQUE in the database, and the PHP insert statement will encounter repeated execution errors. If you catch the errors, you will know that the insertion failed.

  1. After Ajax is submitted, set submit to not clickable

  2. Set a unique index for the database to prevent repeated data

The method is as follows. For more information, see comments.

Var tag = 0; $ ('. xxx '). on ('submit ', function () {// only when tag = 0 Can the submitted event be executed if (tag = 0) {// the first time the submitted event is executed, change the tag value so that when the submit is triggered again, no behavior will be performed for tag = 1; // do something // You can also restore the tag value to 0 when some conditions are met, so that the event can be executed again }})

Click Submit time. After the return result is successful, the content in the form is cleared. This avoids repeated submission.

Refer to laravel csrf-token https://laravel.com/docs/5.1/routing#csrf-x-csrf-token

Frontend
It seems that bootstrap or jquery has a button method.
For example, you can add a data attribute on the button.

Submit
Js:
$ ('# Submit'). button ('submit '). on ()

In this way, the button is automatically disable and the text of data-submit is displayed.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.