Preventing a form to be submitted more than once

Hi

Is there anyway we can prevent a form to be submitted more than once without using a session?

Thanks

PS: I have searched google and all answers i found were using sessions.

Using a session would be the easiest option unless you would prefer to use a database but that would cause overhead which a simple check wouldn’t need.

Although it’s not 100% foolproof, you could use JavaScript to set an onClick event for your form’s submit button, which will disable the button, as soon as it is pressed.

Using jQuery, where formID is the id of your form element:

$('form#formID').submit(function(e){
  $(this).children('input[type=submit]').attr('disabled', 'disabled');
});

I was faced with a similar problem in a recent Rails project where it was very important for the client that applicants didn’t submit their details more than once.
To solve this, I created a unique hash for every user, based on the details they had entered. I stored this hash in the data base and was thus able to avoid saving duplicate entries by comparing hash value.

@chris.upjohn

How would you do this using a session?

Upon each form submission, you’ll have to check to see if a session (set by a previous form submission) exists.

Example:


<?php session_start();

if(isset($_POST['submit']))
{
	if(isset($_SESSION['form_submitted']))
	{
		die('You have already submitted the form.');
	}
	else
	{
		#submitted form code here
		$_SESSION['form_submitted'] = TRUE;
	}
}
?>

This method however will not stop end users from changing their web browser and re-submitting the form again. Also upon session expiration the users will be able to re-submit the form again; so perhaps using a database and storing IP address logs would be a better approach. The method you choose however will really depend on your current setup and how important it is to stop your users from multiple form submits.