Preventing form from resubmitting

Is there a way to keep a form resubmitting upon refresh or back-history that DOESN’T involve: .js, $_SESSION, or header('location:…)?

I am trying to display, process, and respond to a form all in the same script/document. So far everything works splendidly. I have created a class that I can instance for the form.

  1. Checks to see if it is submitted ( via a isset(_whateverMETHOD[‘someName’) )
  2. If the form has not been submitted, it simply displays the form
  3. If a query string (and it’s not submitted) is present, it reads a DB and FILLS the form, for editing.
  4. if it’s submitted, it runs a validation function.
    4a) If validation fails, it clears the bad field, fills the form with the remaining data and displays the from along with error messages
    4b) If validation passes it submits the data to the DB; and displays a ‘success’ ( instead of the form)

The problem is that refreshing the page resubmits the form ( creating duplicate entries in the DB).

I don’t want to include .js in this project, and $_SESSION or hidden fields seem a bit convoluted.

I was thinking that i could easily redirect the page to itself on successful submission, which would solve the refresh issue, but I am concerned that if I do that I wouldn’t be able to display the success message (as the from wouldn’t be able to tell whether this was a new request or redirect, on step 1)

I was hoping there was a way to unset( _whateverMETHOD['someName) in the form request to prevent resubmission or some way to pass a variable forward (other than GET, POST, or SESSION as the form class already employs these) toilet the scrip know the form was submitted and the success message should be displayed ?

I hope I am expressing this clearly. As always all suggestions are greatly appreciated.

The only time that is likely is if your table uses an auto-increment to generate a new key for each. Most of the time there is a specific field that is already a unique key so the problem doesn’t occur.

Where you absolutely have to have an auto-increment you can compare some of the values in the submitted record to the last few entries in the table to see if it duplicates any of them and discard the submitted data if it does.

At least that’s how I have been handling it in my applications.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.