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?
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:
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
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 ..
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.
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.
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>
when I use the php redirect it clears all the variables that I want to keep to send to the payment gateway.
Can u please explain more? after the email is sent, what's next? and what is the form all about? Post the form here
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