#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2011
    Posts
    118
    Rep Power
    50

    Question Submit Form -> Thank You + Email Confirm, later Back = double email


    I have a form that is filled in on a PHP file.

    When submitted, the form reopens itself with the $_POST data.

    After validation and saving to the database, the user is redirected to a ThankYou.php page.

    Simple enough, all works well.

    BUT, I thought, "wouldn't it be nice to send a Thank You Email at the same time as the Thank You page."

    So, the text of the Thank You page is sent to the user as an email at the end of ThankYou.php. Again, works fine.

    Here's the problem. If the user, goes off and does other stuff, then hits back and lands on ThankYou.php it sends the email again. It's not really a problem. It isn't saving anything again, just resending the email. But the email might confuse the user.

    I don't see anything in PHP which lets me detect we got here via BACK? ($_SERVER['HTTP_REFERER'] has the original referer.)

    So, I think I have to refactor and put the ThankYou-Email after the save, but before the ThankYou.php redirect.

    Am I missing an easier way?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,465
    Rep Power
    594
    Use a session variable. Check to see if it is set before sending the email. If it is set, skip sending. If it is not set, send the email and set it.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2011
    Posts
    118
    Rep Power
    50
    Ah yes. Thanks. For some reason the unset part of that solution was alluding me.

    So, basically, I'll do something like this:
    SubmitForm.php
    PHP Code:
    // Check all is good and save 

    $_SESSION['SendUserThankYouEmail']=true
    // Redirect to ThankYou.php 
    ThankYou.php
    PHP Code:
    Thanks 
    if (isset($_SESSION['SendUserThankYouEmail'])) {  
      
    send_email('Thanks'); 
      unset 
    $_SESSION['SendUserThankYouEmail']; 


IMN logo majestic logo threadwatch logo seochat tools logo