atomicc Posted August 6, 2010 Share Posted August 6, 2010 Hello, guys. I'm using the following code to send me an automated mail in case the user find a broken link. But there are 2 problems: 1) after submitting I'd rather not disrupt their game by refreshing the page. So, is there any way to send mail without the post refreshing? 2) if the user hits F5, the page reloads and the code runs again, sending two or more times the same email. How can I avoid duplicated emails caused by manual browser refresh? (the most important solution currently is how to to avoid duplicated entries, in case an ajax solution is too hard to post here.) <form name="fbrlink" action="<?= $_SERVER['PHP_SELF']."?page=ok" ?>" method="post"> <?php echo('<input name="sbrlink" id="B1" type="button" value="broken Link?" style="border:none; background-color:#FFFFFF; font-family:Trebuchet MS; font-size:15px; font-weight:bold; color:#1A4877; cursor:pointer; display:block; text-decoration:underline;" onclick="this.form.submit();"/>'); ?> <?php if ($_SERVER['REQUEST_METHOD'] == 'POST') { if ($_SERVER['QUERY_STRING'] == 'page=ok') { echo '<script type="text/javascript" >confirm("Click OK to notify us.");</script>'; mail("contact@ctt.net","report broken link","Game notified:MarioBros"); echo '<script type="text/javascript" >document.fbrlink.sbrlink.disabled = true;</script>'; ?> <script type="text/javascript" >document.fbrlink.action='<?php echo $a; ?>'; alert('<?php $_SERVER['QUERY_STRING'];?>'); </script> <?php } else {} }else{ } ?> Quote Link to comment https://forums.phpfreaks.com/topic/210015-avoid-duplicated-mails-on-browser-refresh/ Share on other sites More sharing options...
TOA Posted August 7, 2010 Share Posted August 7, 2010 After you send the mail you could set a cookie or session var like $sent = true or something, and test against it everytime. Just my 2 cents Quote Link to comment https://forums.phpfreaks.com/topic/210015-avoid-duplicated-mails-on-browser-refresh/#findComment-1096245 Share on other sites More sharing options...
jcbones Posted August 7, 2010 Share Posted August 7, 2010 You could process your form before page output. Immediately after form processing, refresh the page. Then they can hit f5 all they want, and the form will not process again. Until they submit it, again. Quote Link to comment https://forums.phpfreaks.com/topic/210015-avoid-duplicated-mails-on-browser-refresh/#findComment-1096247 Share on other sites More sharing options...
atomicc Posted August 9, 2010 Author Share Posted August 9, 2010 After you send the mail you could set a cookie or session var like $sent = true or something, and test against it everytime. Just my 2 cents I've never needed a cookie before. How can I use a cookie? You could process your form before page output. Immediately after form processing, refresh the page. Then they can hit f5 all they want, and the form will not process again. Until they submit it, again. jcbones, I didn't understand what you mean. How can I use a submit before a <HTML> tag? how this will prevent the browser update? Thank you for your answers. Quote Link to comment https://forums.phpfreaks.com/topic/210015-avoid-duplicated-mails-on-browser-refresh/#findComment-1096813 Share on other sites More sharing options...
jcbones Posted August 9, 2010 Share Posted August 9, 2010 Ideally all processing should reside outside of HTML output. This way you can manipulate the page the way you would like. You can run this script as a test, to show you what I'm talking about. <?php if(isset($_POST['submit'])) { header('Status: 200'); header('Location: ' . $_SERVER['PHP_SELF'] . '?pass=true'); } elseif(isset($_GET['pass'])) { $message = 'You have successfully posted the form, now hit f5 for refresh.'; } else { $message = 'Please submit the form.'; } ?> <html> <head> <title>Test</title> </head> <body> <?php echo $message; ?> <form action="" method="post"> <input type="text" value="test" name="test"/> <input type="submit" name="submit" value=" SUBMIT " /> </form> Quote Link to comment https://forums.phpfreaks.com/topic/210015-avoid-duplicated-mails-on-browser-refresh/#findComment-1096820 Share on other sites More sharing options...
atomicc Posted August 9, 2010 Author Share Posted August 9, 2010 really, jcjones, the ideal is to use a separate page, but this a game site and there is no need to redirect them to other page, even if it doesn't works. I've put your code at the beginning of the php file, but the button doesn't submit (for example, I can't see the alert generated by js. ) I also have put only the following piece.. echo '<script type="text/javascript" >confirm("Click OK to notify us.");</script>'; mail("contact@ctt.net","report broken link","Game notified:MarioBros"); echo '<script type="text/javascript" >document.fbrlink.sbrlink.disabled = true;</script>'; ...after your phrase $message = 'Please submit the form.'; what causes continuous mail sending, even if I don't click the button submit. where's the error?? Quote Link to comment https://forums.phpfreaks.com/topic/210015-avoid-duplicated-mails-on-browser-refresh/#findComment-1096975 Share on other sites More sharing options...
jj20051 Posted August 9, 2010 Share Posted August 9, 2010 I suggest you setup a session variable. When the form is processed make the variable setup... Example: if($_SESSION['email_sent'] !== 1){ //send email $_SESSION['email_sent'] = 1; } else {} Just my two cents... Quote Link to comment https://forums.phpfreaks.com/topic/210015-avoid-duplicated-mails-on-browser-refresh/#findComment-1096991 Share on other sites More sharing options...
onlyican Posted August 9, 2010 Share Posted August 9, 2010 I will start honestly, I aint bothered reading everything above. All I saw was Mail Form Multiple Refresh Anyway, Are you using any Captchas to stop bots using your form. Most of these fail if the form has been re-submitted (Refreshed) A very popular / Good Captcha, is googles recaptcha (They bought it early this year I think) http://www.google.com/recaptcha Quote Link to comment https://forums.phpfreaks.com/topic/210015-avoid-duplicated-mails-on-browser-refresh/#findComment-1097001 Share on other sites More sharing options...
atomicc Posted August 9, 2010 Author Share Posted August 9, 2010 Thank you, onlyican, your tip is very welcome. I'll give it a try. Tyvm. Quote Link to comment https://forums.phpfreaks.com/topic/210015-avoid-duplicated-mails-on-browser-refresh/#findComment-1097082 Share on other sites More sharing options...
jcbones Posted August 9, 2010 Share Posted August 9, 2010 1. This confirm box. echo '<script type="text/javascript" >confirm("Click OK to notify us.");</script>'; Will not keep this code from running. mail("contact@ctt.net","report broken link","Game notified:MarioBros"); If you would post the entire script, then I would fix it for you. My lessons don't seem to be helping. Quote Link to comment https://forums.phpfreaks.com/topic/210015-avoid-duplicated-mails-on-browser-refresh/#findComment-1097193 Share on other sites More sharing options...
atomicc Posted August 10, 2010 Author Share Posted August 10, 2010 Ok, let's post it. Here's the code: <?php if(isset($_POST['submit'])) { header('Status: 200'); header('Location: ' . $_SERVER['PHP_SELF'] . '?pass=true'); } elseif(isset($_GET['pass'])) { $message = 'You have successfully posted the form, now hit f5 for refresh.'; } else { $message = 'Please submit the form.'; } ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> </head> <body id="index" class="standard" > <form name="fbrlink" action="" method="post"> <p> <?php echo('<input name="sbrlink" id="B1" type="button" value="Broken Link?" style="border:none; background-color:#FFFFFF; font-family:Trebuchet MS; font-size:15px; font-weight:bold; color:#1A4877; cursor:pointer; display:block; text-decoration:underline;" onclick="this.form.submit();"/>'); ?> <?php if ($_SERVER['REQUEST_METHOD'] == 'POST') { if ($_SERVER['QUERY_STRING'] == 'page=ok') { $a=$_SERVER['PHP_SELF']; echo '<script type="text/javascript" >confirm("Click OK to notify us.");</script>'; mail("contact@ctt.net","report broken link","Game notified:MarioBros"); echo '<script type="text/javascript" >document.fbrlink.sbrlink.disabled = true;</script>'; ?> <script type="text/javascript" >document.fbrlink.action='<?php echo $a; ?>'; alert('<?php $_SERVER['QUERY_STRING'];?>'); </script> <?php } else {} }else{ } ?> </p> </form> </body> </html> Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/210015-avoid-duplicated-mails-on-browser-refresh/#findComment-1097511 Share on other sites More sharing options...
jcbones Posted August 10, 2010 Share Posted August 10, 2010 Try this, and report back, ... Please. <?php $javascript = NULL; if(isset($_POST['sbrlink'])) { if(mail("contact@ctt.net","report broken link","Game notified:MarioBros")) { header('Status: 200'); header('Location: ' . $_SERVER['PHP_SELF'] . '?pass=true'); exit(); } else { $message = 'There was an error, when trying to contact us. Please try again.'; } } elseif(isset($_GET['pass'])) { $message = 'Thanks for the notification, we will address it shortly. Sorry for your inconvienence.'; $javascript = '<script type="text/javascript" >document.getElementById(\'sbrlink\').disabled = true;</script>'; } else { $message = NULL; } ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> </head> <body id="index" class="standard" > <form id="fbrlink" action="" method="post" onsubmit="return confirm('Click OK to notify us.');"> <p> <?php echo $message; ?> <input id="sbrlink" name="sbrlink" type="submit" value="Broken Link?" style="border:none; background-color:#FFFFFF; font-family:Trebuchet MS; font-size:15px; font-weight:bold; color:#1A4877; cursor:pointer; display:block; text-decoration:underline;" /> <?php echo $javascript; ?> </p> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/210015-avoid-duplicated-mails-on-browser-refresh/#findComment-1097819 Share on other sites More sharing options...
atomicc Posted August 11, 2010 Author Share Posted August 11, 2010 Thanks a million. You're the man. It works fine now. wtg, jcbones!! Quote Link to comment https://forums.phpfreaks.com/topic/210015-avoid-duplicated-mails-on-browser-refresh/#findComment-1098067 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.