PHPBuilder - Avoid multiple form submissions

RSS Twitter

Avoid multiple form submissions

by: PHP Builder Staff
April 17, 2012

Does your database suffer from "duplicate post" syndrome? The cure isn't too difficult. Here is a simple way to prevent users from submitting the same form multiple times.

First, declare a session variable to store a serial number for each form. I call mine "$userLastAction." Then, in every form where duplicate submission is a problem, include a hidden field, and set the value to $userLastAction+1:

<INPUT TYPE=HIDDEN NAME=lastAction VALUE=<?= $userLastAction+1 ?>>

Finally, verify that the form has not been previously submitted before acting on the submission:

if($lastAction>$userLastAction and inputIsValid(...)){
  $userLastAction++; // Increment serial number
  // Act on form here

Comment and Contribute

Your comment has been submitted and is pending approval.




(Maximum characters: 1200). You have characters left.