mail problems - PLEASE HELP!

Discussion in 'PHP' started by sjlogan88, Nov 14, 2008.

  1. #1
    I've set up a php script to process and send emails from a form on a website. However whenever they refresh the page with the script on it sends them the email again.

    Whats the best way to get the script to only send one email? like a send_once function?
     
    sjlogan88, Nov 14, 2008 IP
  2. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #2
    After the email function, redirect the user back to the page again (to clear the submitted data)

    using javascript:
    <script type="text/javascript">
    window.location = window.location
    </script>
    HTML:
    or php header()
    
    header('location: thefile.php');
    
    PHP:
    If you want to show a success message after the email is sent. Try this after the email function:
    
    header('location: thefile.php?success');
    
    PHP:
    Then use this code at where you want the message to be shown:
    
    if (isset($_GET['success'])) {
    print 'Email has been sent.';
    }
    
    PHP:
     
    ads2help, Nov 14, 2008 IP
  3. penalty

    penalty Member

    Messages:
    36
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #3
    hm, generate a unique id for the user and save the id into a $_SESSION['check'] variable everytime he sends the form.

    add a hidden field to the form with the value of the $_SESSION['check'] variable

    put a if($_POST['hidden'] != $_SESSION['check']) {send the mail} { else { echo "already sent"; }

    this might work
     
    penalty, Nov 14, 2008 IP
  4. SoundRoom

    SoundRoom Peon

    Messages:
    26
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    After success ( email sent ) add a simple redirection to the main page ( index.php ) ! It will clear all variables and will not send email again ..
     
    SoundRoom, Nov 14, 2008 IP
  5. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #5
    This would require extra work because how if the user want to send another email again? You would have to reset or regenerate a unique id?

    Just simply redirect to the same page again will do.
     
    ads2help, Nov 14, 2008 IP
  6. sjlogan88

    sjlogan88 Peon

    Messages:
    74
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #6
    What would you have to do if you wanted to keep the variables like name for another form but just didn't want the email to be sent again. Could you create a form page set it to a process page which would then send out the emails and then redirect to a page which would store the details and payment information from the previous form page? Sorry if I'm making no sense lol.
     
    sjlogan88, Nov 14, 2008 IP
  7. SoundRoom

    SoundRoom Peon

    Messages:
    26
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    <?php
    echo "Do some functions here ..";
    header("Location: redirect_here.php");
    ?>
    Code (markup):
     
    SoundRoom, Nov 14, 2008 IP
  8. nepasolutions

    nepasolutions Peon

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    you better redirect the page so it will not send same email again and again on refreshing page. you can redirect the page using this pho code header("location:redirect.php") or you can do it with javascript also like this way
    <script type="text/javascript">
    location.href = "redirect.php"
    </script>
     
    nepasolutions, Nov 14, 2008 IP
  9. sjlogan88

    sjlogan88 Peon

    Messages:
    74
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #9
    will that not clear all the variables below which I'm trying to post to the next page?
     
    sjlogan88, Nov 14, 2008 IP
  10. chandan123

    chandan123 Prominent Member

    Messages:
    11,586
    Likes Received:
    578
    Best Answers:
    0
    Trophy Points:
    360
    #10
    redirect to another page is simple and efficient :)
     
    chandan123, Nov 14, 2008 IP
  11. sjlogan88

    sjlogan88 Peon

    Messages:
    74
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #11
    when I use the php redirect it clears all the variables that I want to keep to send to the payment gateway.
     
    sjlogan88, Nov 14, 2008 IP
  12. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #12
    Can u please explain more? after the email is sent, what's next?

    and what is the form all about? Post the form here
     
    ads2help, Nov 14, 2008 IP
  13. xenous

    xenous Peon

    Messages:
    102
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #13
    after sendmail command add a session variable as $_SESSION[mail_sent]=1;
    and have ur code like that
    if($_SESSION[mail_sent]==1):
    //do nothing
    else:
    mail(......) //your sendmail code
    $_SESSION[mail_sent]=1;
    endif;

    *** Edit***
    Definately thinking you have a session running
     
    xenous, Nov 14, 2008 IP