Php solves and avoids repeated form submission

Source: Internet
Author: User
: This article mainly introduces how to solve php and avoid repeated submission of form forms. For more information about PHP tutorials, see. Reproduced in: http://www.hlmblog.com/183.html

When submitting a form, the page may suddenly slow down due to the speed of the network. if you click the submit button repeatedly, multiple data entries will be generated in the database, resulting in uncontrollable situations.

For example, in the following case, the form will be submitted repeatedly:
Click submit twice.
Click refresh.
Use the browser back button to repeat the previous operation, resulting in repeated form submission.
Use the browser history to repeat the submission form.
Repeated HTTP requests in the browser.
Webpages are maliciously refreshed.

Below are several solutions:
I. click the js setting button and it turns gray.

$(document).ready(function(){  $(input:submit).click(){      setTimeout(function(){obj.disabled=true;},100)  };});

II. use session

The hidden form field stores sessions (the tags generated when the form is requested ).

After receiving form data, use this method to check whether the flag value exists. first, delete the value and process the data. if the value does not exist, it indicates that it has been submitted. ignore this submission.

// The server generates a random number and saves it to the session. it is allocated to the table single page $ data ['sess _ id'] = $ _ SESSION ['Sid '] = mt_rand (1000,999 9 ); $ this-> load-> view ('form', $ data); // hide the field on a single table page to store this session value// Process if ($ _ POST ['Sid ']! = ''& $ _ POST ['Sid '] = $ _ SESSION ['Sid']) {unset ($ _ SESSION ['Sid ']); echo 'process data';} else {echo 'submitted form ';}

III. use cookies
The principle is similar to session, but once the user's browser disables cookies, this function becomes invalid.

If (isset ($ _ POST ['submit ']) {setcookie ("tempcookie", "", time () + 30); header ("Location :". $ _ SERVER [PHP_SELF]); exit ();} if (isset ($ _ COOKIE ["tempcookie"]) {setcookie ("tempcookie", "", 0 ); echo "you have submitted a form ";}

4. jump using the header function

Once the user clicks the submit button, after the data is processed, the page jumps to another page.

If (isset ($ _ POST ['submit ']) {header ('Location: success. php'); // after processing data, go to another page}

5. use the database to add constraints

Directly add a unique constraint to the database or create a unique index. Once the user commits the statement repeatedly, a warning or prompt is thrown,
This is the most direct and effective method for processing only the data submitted for the first time. it is required that the database design and architecture at the early stage be fully considered.

The above describes how to solve php and avoid repeated submission of form forms, including some content, and hope to help those who are interested in PHP tutorials.

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.