#native_company# #native_desc#
#native_cta#

Avoid multiple form submissions

By PHP Builder Staff
on 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
}