Jump to content
SF23103

preventing form confirmation email on page reload

Recommended Posts

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;
        }
    }

Share this post


Link to post
Share on other sites

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.
  • Like 1

Share this post


Link to post
Share on other sites

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.

  • Like 1

Share this post


Link to post
Share on other sites

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!  

Share this post


Link to post
Share on other sites

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.