SF23103 Posted October 3, 2016 Share Posted October 3, 2016 I may be going about this all wrong, so hopefully someone can help me out! I have an order form that processes with Braintree. If the transaction is successful, it displays a success message with the order number, and calls a different php file that sends a confirmation email. If the transaction is not successful, it displays error messages. The issue I found was that if the transaction is successful, and the user reloaded the page, it would re-send the confirmation email. This could happen repeatedly. To prevent that, I thought I would add a $order_complete variable. In the original success check, I would check to see if order_complete was "1". If not, display the success text, send the email, and set order_complete to "1". If the page was reloaded and order_complete was "1" it would not re-display the success message and not send the email. Else, it would display the errors. Unfortunately this isn't working. It still sends an email every time! Any suggestions? if (($result->success) && $order_complete != "1") { $_POST['transaction_id'] = $result->transaction->id; echo "<p style=\"color:#000; background-color:#008000; padding:20px;width:70%;\">Your order was successful. Thank you for your support!<br/><br/>"; echo("Your transaction ID is: " . $result->transaction->id . "<br /><a href=\"index.php#go_to_form\"> Click here to order again!</a> </p>"); $order_complete = "1"; include ('/path/to/send_mail.php'); } else if (($result->success) && $order_complete == "1") { echo "Place an Order"; } else { foreach (($result->errors->deepAll()) as $error) { $braintreeError[] = $error->message; } } Quote Link to comment https://forums.phpfreaks.com/topic/302274-preventing-form-confirmation-email-on-page-reload/ Share on other sites More sharing options...
Solution Jacques1 Posted October 3, 2016 Solution Share Posted October 3, 2016 And the $order_complete variable comes from where? You realize that variables only exist while the script is running, right? When the page is reloaded, the variables from the previous run are long dead. The overall approach seems very odd, so it might be a good idea to start from scratch: Braintree appearently uses POST requests to confirm orders. So you need a script which reacts specifically to POST requests (see $_SERVER['REQUEST_METHOD']), not plain GET requests. When you receive a (positive) confirmation, you send an e-mail. But don't use the strange approach of including some other script which then sends the e-mail. This is extremely confusing, and I bet that requesting the script directly in the browser causes trouble. If you don't want to have the e-mail code directly in the main script (which makes sense), define a function or method and then call it in the main script. After the confirmation has been processed, you redirect the client to a status page which merely displays the success/failure message. Since the status page doesn't do anything, the client can reload it as often as they want. See the PRG Pattern for a more detailed explanation. 1 Quote Link to comment https://forums.phpfreaks.com/topic/302274-preventing-form-confirmation-email-on-page-reload/#findComment-1537993 Share on other sites More sharing options...
ginerjm Posted October 3, 2016 Share Posted October 3, 2016 And how is $order_complete being saved between script executions? Why not post the signal to the order record in your db? As it is it does not appear to be a value that is being retained. 1 Quote Link to comment https://forums.phpfreaks.com/topic/302274-preventing-form-confirmation-email-on-page-reload/#findComment-1537994 Share on other sites More sharing options...
SF23103 Posted October 3, 2016 Author Share Posted October 3, 2016 Thank you for the direction! I've totally scrapped that horrible idea haha. I am now following PRG after the mail is sent if it's successful. I've also combined the mail code in there instead of using an include. Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/302274-preventing-form-confirmation-email-on-page-reload/#findComment-1538000 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.