doubledee Posted April 28, 2011 Share Posted April 28, 2011 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?! 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 Quote Link to comment https://forums.phpfreaks.com/topic/234940-form-being-double-submitted/ Share on other sites More sharing options...
doddsey_65 Posted April 28, 2011 Share Posted April 28, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/234940-form-being-double-submitted/#findComment-1207415 Share on other sites More sharing options...
PFMaBiSmAd Posted April 28, 2011 Share Posted April 28, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/234940-form-being-double-submitted/#findComment-1207419 Share on other sites More sharing options...
doubledee Posted May 2, 2011 Author Share Posted May 2, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/234940-form-being-double-submitted/#findComment-1209654 Share on other sites More sharing options...
BloodyMind Posted May 3, 2011 Share Posted May 3, 2011 you can set a session variable, that the order with ID XXXXX was processed or reached some state, and skip the process to the next step Quote Link to comment https://forums.phpfreaks.com/topic/234940-form-being-double-submitted/#findComment-1210149 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.