Jump to content

Form being double submitted


doubledee

Recommended Posts

I've got a BIG problem...

 

When a user submits my form it works fine, displays a "Transaction Success/Failed", and e-mails me a confirmation.

 

However, if the user then navigates to another page (e.g. "Home"), and then clicks their browser's "Back" button, my form gets re-submitted?!  :o  :o

 

This is on a VPS, but I just chatted with server support and they are saying,

 

register_globals = Off

 

So what is going wrong?!

 

 

 

Debbie

 

 

Link to comment
Share on other sites

Im assuming there would be an alert when pressing the back button saying the data would be resent. This is normal. Its why sites redirect to a confirmation page when a form is submitted. If it stayed on the same page and the user hit refresh then it would also resend the data.

Link to comment
Share on other sites

The problem occurs because the browser's history recorded for the URL is a form submission. When you navigate back to that URL the browser attempts to perform the action it has recored for that URL. There are two things you can do to fix this -

 

1) After you have successfully processed the form submission, redirect to the same URL. This will cause a GET request for that URL to be recored in the browser's history and it won't resubmit the form data when you navigate back to that URL.

 

2) Store a value in a session variable that indicates that the form has been processed and skip the form processing code as long as that session variable is set.

Link to comment
Share on other sites

Here a really crude, stripped down version of my form...

 

<?php
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head></head>

<body>
<!-- Access Constants -->
<?php	//require_once('../config.inc.php');	?>

<div id="wrapper" class="clearfix">
	<div id="inner">
		<!-- Include BODY HEADER -->
			<?php	//require_once(ROOT . 'components/body_header.inc.php');	?>

		<!-- PAYMENT FORM -->
		<div id="paymentForm">
			<h1>Concert Registration Form</h1>


			<?php
				// Initialize variables.
				$attendeeName = $form_value = '';
				$form_value = $_POST['form_value'];
				echo '<br />CURRENT POST[form_value] = ' . $form_value;
				echo '<br />CURRENT SESSION[form_value] = ' . $_SESSION['form_value'] . '<br />';
				// *****************************************************************
				// HANDLE FORM.
				// *****************************************************************
				if ((isset($_POST['submitted'])) && ($form_value == $_SESSION['form_value'])){
					// Submitted.
					echo '<br />** Processing Form **<br />';
					echo '<br />** Unsetting Session variable **<br />';
					unset($_SESSION['form_value']);

					// Check Payment Info.

					// Check for Errors.
					if (empty($errors)){
						// ***************************************************************
						// PROCESS PAYMENT.
						// ***************************************************************

						echo "			</div>";	// End of PAYMENT FORM
						echo "		</div>";	// End of #INNER
						echo "	</div>";	// End of #WRAPPER

							//<!-- Include BODY FOOTER -->
							//require_once(ROOT . 'components/body_footer.inc.php');

						echo "</body>";
						echo "</html>";

						// Do not return to Payment Form!!!
						exit();
						// ***************************************************************
					}// End of CHECK FOR ERRORS.
				}else{
					echo '<br />** CANNOT RE-DISPLAY FORM **<br />';
					// End of HANDLE FORM.
				}
			?>

<!-- NEW -->
<?php
$_SESSION['form_value'] = rand(1, 1000);									// NEW
//	$_SESSION['hidden_value'] = md5(uniqid(rand(), true));									// NEW
echo 'SET NEW SESSION = ' . $_SESSION['form_value'] . '<br />';			// NEW
?>

			<!-- @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ -->
			<!-- HTML PAYMENT FORM -->
			<form id="payment" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">


<!-- NEW -->
<label>SET NEW FORM VALUE:</label>
<input type="input" name="form_value" id="form_value"
			 value="<?php echo $_SESSION['form_value'];?>" />


				<!-- ***************************************************************** -->
				<!-- CONCERT DETAILS -->
				<fieldset>
					<legend>Concert Details</legend>
					<ol>
						<!-- Attendee Name -->
						<li>
							<label for="attendeeName">Attendee Name:</label>
							<input id="attendeeName" name="attendeeName" class="text" type="text"
										 maxlength="30" value="<?php echo $attendeeName; ?>" />
						</li>
					</ol>
				</fieldset>

				<!-- Submit Form -->
				<fieldset id="submit">
					<input name="submit" type="image" src="../images/PlaceOrder_bk.png" value="Place Order" />
					<input name="submitted" type="hidden" value="true" />
				</fieldset>
		</div><!-- End of PAYMENT FORM -->

	</div><!-- End of #INNER -->
</div><!-- End of #WRAPPER -->

<!-- Include BODY FOOTER -->
<?php	//require_once(ROOT . 'components/body_footer.inc.php');	?>
</body>

</html>

 

 

I can't seem to get this working correctly...

 

 

 

Debbie

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.