Stop Multiple Submission on forms

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
websak
Forum Newbie
Posts: 4
Joined: Wed Jul 07, 2010 10:29 am

Stop Multiple Submission on forms

Post by websak »

Hello there

Can anyone help me i am trying to figure a way where to stop multiple submission on my form that i am creating can anyone please please help me i am at loss of what to do, I would appreciate some help with this

Cheers

Stuart

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">	
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; utf-8" />
		<title>Penny Lane Contact Form</title>
		<script src="http://code.jquery.com/jquery-latest.js"></script>
 	    <script type="text/javascript" src="http://dev.jquery.com/view/trunk/plugins/validate/jquery.validate.js"></script>
		 <script>
 		 $(document).ready(function(){
   			 $("#contactForm").validate();
  			});
  			</script>
		
	</head>	

<body>

<style type="text/css">
   
   input { margin: .2em 10px;}
   label, input, select { float: left;}
   select { margin-left: 10px;}
   label { width: 250px;}
   .error { color: #EE0000;}
   
</style>

<h2>Contact Form</h2>

<?

include_once "inc/database.php";


?>

<?
/*CONTACT FORM*/

$register = false;

if ($_POST[action] == "send"){
	
	$register= true;	

	if ($_POST[fld_name] == "" || $_POST[fld_tel] == "" || $_POST[fld_callback] == "" || $_POST[fld_newsletter] == "") $errmsg = "Please fill in all starred fields";
}

if ($register){
	$yesorno = "NO";
		if ($_POST[fld_newsletter] == "YES") $yesorno = "YES";
	$query = "INSERT INTO Contact (ContactName, ContactEmail, ContactTel, ContactTime, ContactHear, Newsletter)
			  VALUES ('{$_POST[fld_name]}','{$_POST[fld_email]}','{$_POST[fld_tel]}','{$_POST[fld_callback]}','{$_POST[fld_hear]}','$yesorno')";

//print "running sql query: $query";
	(mysql_query($query, $link));
	
	$email ="stuartk@triad.uk.com";
	$subject="Contact Enquiry";
	$message = "CONTACT ENQUIRY-----------------------------------------\n
The following person $_POST[fld_name] would like to be contacted\n
Email Address: $_POST[fld_email]\n
Telephone Number: $_POST[fld_tel]\n
I would like to be called back at: $_POST[fld_callback]\n
Where did you hear about us: $_POST[fld_hear]\n
Do you want to sign up for the newsletter: $_POST[fld_newsletter]";
	mail($email, $subject, $message, "From: $email");

}

?>	
	<form action="/contact.php" method="post" id="contactForm">
		<fieldset>	
			<label for="fld_name">Contact Name</label>
			<input type="text" name="fld_name" value="<?=$_POST[fld_name]?>" class="required"></input><br clear="all" />
			
			<label for="fld_email">Contact Email</label>
			<input type="text" name="fld_email" value="<?=$_POST[fld_email]?>" class="required"></input><br clear="all" />
			
			<label for="fld_tel">Contact Telephone Number</label>
			<input type="text" name="fld_tel" value="<?=$_POST[fld_tel]?>" class="required"></input><br clear="all" />
			
			<label for="fld_callback">Call Back Time</label>
			<select id="fld_callback" name="fld_callback"/>
	               <option value="x">Please Select...</option>
	               <option value="9am to 12pm" <? if ($_POST[fld_callback] == "9am to 12pm") print " selected"; ?>>Morning 9am to 12pm</option>
	               <option value="12pm to 4pm" <? if ($_POST[fld_callback] == "12pm to 4pm") print " selected"; ?>>Afternoon 12pm to 4pm</option>
	               <option value="6pm to 9pm" <? if ($_POST[fld_callback] == "6pm to 9pm") print " selected"; ?>>Evening 6pm to 9pm</option>
              	 </select><br clear="all" />
              	 
			<label for="fld_hear">Where did you hear about us?</label>
			<input type="text" name="fld_hear" value="<?=$_POST[fld_hear]?>"></input><br clear="all" />
			
			<label for="fld_newsletter">Do you want to sign up for the newsletter</label>
			<select id="fld_newsletter" name="fld_newsletter"/>
			<option value="x">Please Select...</option>
	               <option value="YES" <? if ($_POST[fld_newsletter] == "YES") print " selected"; ?>>YES</option>
	               <option value="NO" <? if ($_POST[fld_newsletter] == "NO") print " selected"; ?>>NO</option>
	        </select><br clear="all" />
	        <input type="submit" value="Submit" name="submit" class="submitButton"/>
		</fieldset>	
			<input type="hidden" value="send" name="action"/>
	</form>
	
</body>
</html>	
	



User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: Stop Multiple Submission on forms

Post by Jade »

Use sessions. Once the form has been submitted change the session variable. That way when they come back to the form you can check to see if the session variable exists and if it does then they've already filled out the form.
websak
Forum Newbie
Posts: 4
Joined: Wed Jul 07, 2010 10:29 am

Re: Stop Multiple Submission on forms

Post by websak »

Ok Great thankyou

I am not very familar with sessions can you assist at all?
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: Stop Multiple Submission on forms

Post by Jade »

Code: Select all

<?php
session_start(); //start using sessions, must always be at the top of the page with no output before it
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">     
<html>
        <head>
                <meta http-equiv="Content-Type" content="text/html; utf-8" />
                <title>Penny Lane Contact Form</title>
                <script src="http://code.jquery.com/jquery-latest.js"></script>
            <script type="text/javascript" src="http://dev.jquery.com/view/trunk/plugins/validate/jquery.validate.js"></script>
                 <script>
                 $(document).ready(function(){
                         $("#contactForm").validate();
                        });
                        </script>
               
        </head>

<body>

<style type="text/css">
   
   input { margin: .2em 10px;}
   label, input, select { float: left;}
   select { margin-left: 10px;}
   label { width: 250px;}
   .error { color: #EE0000;}
   
</style>

<h2>Contact Form</h2>
<?
include_once "inc/database.php";

/*CONTACT FORM*/

$register = false;

if ($_POST[action] == "send"){
       
        $register= true;       

        if ($_POST[fld_name] == "" || $_POST[fld_tel] == "" || $_POST[fld_callback] == "" || $_POST[fld_newsletter] == "") $errmsg = "Please fill in all starred fields";
}

if ($register){
        $yesorno = "NO";
                if ($_POST[fld_newsletter] == "YES") $yesorno = "YES";
        $query = "INSERT INTO Contact (ContactName, ContactEmail, ContactTel, ContactTime, ContactHear, Newsletter)
                          VALUES ('{$_POST[fld_name]}','{$_POST[fld_email]}','{$_POST[fld_tel]}','{$_POST[fld_callback]}','{$_POST[fld_hear]}','$yesorno')";

//print "running sql query: $query";
        (mysql_query($query, $link));
       
        $email ="stuartk@triad.uk.com";
        $subject="Contact Enquiry";
        $message = "CONTACT ENQUIRY-----------------------------------------\n
The following person $_POST[fld_name] would like to be contacted\n
Email Address: $_POST[fld_email]\n
Telephone Number: $_POST[fld_tel]\n
I would like to be called back at: $_POST[fld_callback]\n
Where did you hear about us: $_POST[fld_hear]\n
Do you want to sign up for the newsletter: $_POST[fld_newsletter]";
        mail($email, $subject, $message, "From: $email");

      //now we create the session, if they return to this page the session
      //data will be available and you can check to see what the value of this is
       $_SESSION['registered'] = true;
}

//we're checking for existing session data
if ($_SESSION['registered'])
   echo "<center>You've already registered.</center>";
else
{
?>     
        <form action="/contact.php" method="post" id="contactForm">
                <fieldset>     
                        <label for="fld_name">Contact Name</label>
                        <input type="text" name="fld_name" value="<?=$_POST[fld_name]?>" class="required"></input><br clear="all" />
                       
                        <label for="fld_email">Contact Email</label>
                        <input type="text" name="fld_email" value="<?=$_POST[fld_email]?>" class="required"></input><br clear="all" />
                       
                        <label for="fld_tel">Contact Telephone Number</label>
                        <input type="text" name="fld_tel" value="<?=$_POST[fld_tel]?>" class="required"></input><br clear="all" />
                       
                        <label for="fld_callback">Call Back Time</label>
                        <select id="fld_callback" name="fld_callback"/>
                       <option value="x">Please Select...</option>
                       <option value="9am to 12pm" <? if ($_POST[fld_callback] == "9am to 12pm") print " selected"; ?>>Morning 9am to 12pm</option>
                       <option value="12pm to 4pm" <? if ($_POST[fld_callback] == "12pm to 4pm") print " selected"; ?>>Afternoon 12pm to 4pm</option>
                       <option value="6pm to 9pm" <? if ($_POST[fld_callback] == "6pm to 9pm") print " selected"; ?>>Evening 6pm to 9pm</option>
                 </select><br clear="all" />
                 
                        <label for="fld_hear">Where did you hear about us?</label>
                        <input type="text" name="fld_hear" value="<?=$_POST[fld_hear]?>"></input><br clear="all" />
                       
                        <label for="fld_newsletter">Do you want to sign up for the newsletter</label>
                        <select id="fld_newsletter" name="fld_newsletter"/>
                        <option value="x">Please Select...</option>
                       <option value="YES" <? if ($_POST[fld_newsletter] == "YES") print " selected"; ?>>YES</option>
                       <option value="NO" <? if ($_POST[fld_newsletter] == "NO") print " selected"; ?>>NO</option>
                </select><br clear="all" />
                <input type="submit" value="Submit" name="submit" class="submitButton"/>
                </fieldset>    
                        <input type="hidden" value="send" name="action"/>
        </form>
<?php
} //end already registered else statement
session_write_close(); //end the sessions
?>
</body>
</html>
websak
Forum Newbie
Posts: 4
Joined: Wed Jul 07, 2010 10:29 am

Re: Stop Multiple Submission on forms

Post by websak »

thanks for that looks great,
another question is how do you stop them for resubmitting for a certain amount of time say 30 secs or so, if that makes sense.
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: Stop Multiple Submission on forms

Post by Jade »

I'm not exactly sure why you would want to do that if the whole point is to prevent them from submitting the form multiple times. Create another session variable and put the current time() in it when the person submits the form. Then you can check check how much time has passed when they return to the site again and unset the session values if time is up.

http://www.php.net/manual/en/function.session-unset.php
http://us2.php.net/manual/en/function.time.php
websak
Forum Newbie
Posts: 4
Joined: Wed Jul 07, 2010 10:29 am

Re: Stop Multiple Submission on forms

Post by websak »

the reason i asked that question was if they have made a mistake and need to go back to the form and resubmit?
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: Stop Multiple Submission on forms

Post by Jade »

What kind of form is this? Typically if I fill out a form online and realize I made a mistake I just say to heck with it and move on. You probably won't have a lot of people who are going to take the time of day to sit there and go "darn I spelled my last name wrong let me go try and fill out that form again with the right one."

If you really want them to be able to enter the form data again then just add a link that will remove the session values.

Code: Select all

<?php
session_start();

if ($_GET['resetform']) //they want to fill out the form again, clear out the session
{
     session_write_close();
     session_destroy(); 
}
?>
<a href="?resetform=1">Click here to fill out this form again</a>
Post Reply