drewman Posted June 12, 2007 Share Posted June 12, 2007 Hello, I am using this little send to a friend script and although it works well the post data issues on browser refresh are annoying. I would like to find some way of clearing the post data once the form was submitted. Here is the code: <?php /********************************************************* This Free Script was downloaded at Free-php-Scripts.net (HelpPHP.net) This script is produced under the LGPL license Which is included with your download. Not like you are going to read it, but it mostly States that you are free to do whatever you want With this script! NOTE: Linkback is not required, but its more of a show of appreciation to us. *********************************************************/ //Include configuration file and function file //(default location is in the same directory) include_once('../config.php'); //Function to check for valid email function is_valid_email($string) { return preg_match('/^[.\w-]+@([\w-]+\.)+[a-zA-Z]{2,6}$/', $string); } //Check cur page if($_POST['this_page'] == NULL){$_POST['this_page'] = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF'];} //If contact is being sent: if($_POST['submit_id'] == 1){ //Check name entered if($_POST['name'] == NULL){ $message = 'Please enter your name.';} //check if email is enetered if($message == NULL && $REQUIRE_MESS == 1 && $_POST['message_text'] == NULL){ $message = 'Please enter a comment.';} //check if message is entered if($message == NULL && $REQUIRE_MESS == 1 && $_POST['message_text'] == NULL){ $message = 'Please enter a comment.';} //check to send emails if($message == NULL) { if($MULTIPLE_EMAILS == 1){ //multiple emails $valid_emails = array(); $emails = explode(',',$_POST['toemails']); foreach($emails as $email){ $email = trim($email); if($email != NULL){ if(is_valid_email($email) == false ){ $temp_message .= '<br/>We couldn\'t send and email to: <font color="#0000FF">'.$email.'</font> because it is not valid.'; } else { array_push($valid_emails,$email); } } } if(count($valid_emails) <=0 ){ $message = 'Please enter at least one email to send message to.'; } } else { //one email if(is_valid_email($_POST['toemails']) == false ){ $message = 'Please enter a valid friend\'s email.';} } } //End verifications, start processing if($message == NULL){ //compose user message templates replaces $do_search = array('$+name+$','$+page_send+$','$+message_text+$'); $do_replace = array($_POST['name'],$_POST['this_page'],$_POST['message_text']); $subject = str_replace($do_search,$do_replace,$EMAIL_TEMPLATE); //Set Headers $headers = "Return-Path: ".$EMAIL_OPTIONS['TITLE']." <".$EMAIL_OPTIONS['FROM'].">\r\n"; $headers .= "From: ".$EMAIL_OPTIONS['TITLE']." <".$EMAIL_OPTIONS['FROM'].">\r\n"; $headers .= "Content-Type: ".$EMAIL_OPTIONS['TYPE']."; charset=".$EMAIL_OPTIONS['CHARSET'].";\n\n\r\n"; if($MULTIPLE_EMAILS == 1){ foreach($valid_emails as $this_email){ mail ($this_email,$EMAIL_OPTIONS['EMAIL_SUBJECT'],$subject,$headers); $one_pass = true; } } else { mail ($_POST['toemails'],$EMAIL_OPTIONS['EMAIL_SUBJECT'],$subject,$headers); } $message = 'Your emails have been sent, thank you.'; $message .= $temp_message; $_POST = NULL; } } if($message != NULL){ ?> <?php } ?> <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" name="contact" id="contact" style="display:inline;"> <table width="84%" border="0" align="left" cellpadding="0" cellspacing="0"> <tr> <td width="16%" class="pclass">Your name: </td> <td width="31%"><input name="name" type="text" class="selectclass" id="name" value="<?php echo $_POST['name'];?>" size="20" maxlength="50" /></td> <td><?php if($MULTIPLE_EMAILS == 1){?> <span class="pclass">Your Friend's email(s):</span><br/> <em class="pclass">(separated by comma)</em> <?php } else {?> <span class="pclass">Your Friend's email:</span> <?php } ?></td> </tr> <?php if($REQUIRE_EMAIL == 1){?> <tr> <td width="16%" class="pclass">Your Email: </td> <td width="31%"><input name="email2" type="text" class="selectclass" id="email2" value="<?php echo $_POST['email'];?>" size="25" maxlength="50" /></td> <td width="41%"><input name="toemails" type="text" class="selectclass" id="toemails" value="<?php echo $_POST['toemails'];?>" size="20" maxlength="50" /></td> </tr> <?php } ?> <tr> <td width="16%"><span class="pclass">Message:</span></td> <td width="31%"><input name="message_text" type="text" class="selectclass" id="message_text" value="<?php echo $_POST['message_text'];?>" size="25" /></td> <td colspan="2"><input type="submit" name="Submit" value="Send" /> <input name="clear" type="reset" value="clear"> <input name="submit_id" type="hidden" id="submit_id" value="1" /> <input name="this_page" type="hidden" id="this_page" value="<?=$_POST['this_page'];?>" /></td> </tr> <?php if($REQUIRE_MESS == 1){?> <?php } ?> </table> </form> </div><div id="sendfriend_message"><?=$message;?></div> </div></div> </div> </body> </html> I heard something about using MD5 to store unique ids and tell the form not to submit once submitted, but I do not know how to implement this. Any ideas anyone? Quote Link to comment https://forums.phpfreaks.com/topic/55276-clearing-post-data-browser-refresh-re-submits-email-form/ Share on other sites More sharing options...
paul2463 Posted June 12, 2007 Share Posted June 12, 2007 the other way to do it is to not post to itself but use another page which sends the email and then use <?php header("Location: http://www.example.com/"); /* Redirect browser */ /* Make sure that code below does not get executed when we redirect. */ exit; ?> on that page to bring it back, all the post data then is dumped because the data is not posted back and all the fields will be empty Quote Link to comment https://forums.phpfreaks.com/topic/55276-clearing-post-data-browser-refresh-re-submits-email-form/#findComment-273237 Share on other sites More sharing options...
obsidian Posted June 12, 2007 Share Posted June 12, 2007 the other way to do it is to not post to itself but use another page which sends the email and then use <?php header("Location: http://www.example.com/"); /* Redirect browser */ /* Make sure that code below does not get executed when we redirect. */ exit; ?> on that page to bring it back, all the post data then is dumped because the data is not posted back and all the fields will be empty Actually, even with a header redirect like that, if you refresh your page, the information will be requested to be resent (it may be a browser thing, though). I typically use this header redirection technique to send the user to a confirmation page that in turn uses a Javascript redirect back to their home page (or wherever). The javascript redirection actually gives you another entry into your browser history, so unless they actually go "back" a page, they can refresh 'til their heart's content. Quote Link to comment https://forums.phpfreaks.com/topic/55276-clearing-post-data-browser-refresh-re-submits-email-form/#findComment-273241 Share on other sites More sharing options...
drewman Posted June 12, 2007 Author Share Posted June 12, 2007 Is there any other way, as I like how the <?=$message;?> appears on the same page, without flipping back and forth between pages. But if this is the only way, do I change the <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" name="contact" id="contact" style="display:inline;"> to say a process.php that will look like <?php header("Location: " . $_SERVER['HTTP_REFERER']); /* Redirect browser */ ?> then insert a javascript on this page to go back to the main page? What happens to the message code here <?php /********************************************************* This Free Script was downloaded at Free-php-Scripts.net (HelpPHP.net) This script is produced under the LGPL license Which is included with your download. Not like you are going to read it, but it mostly States that you are free to do whatever you want With this script! NOTE: Linkback is not required, but its more of a show of appreciation to us. *********************************************************/ //Include configuration file and function file //(default location is in the same directory) include_once('../config.php'); //Function to check for valid email function is_valid_email($string) { return preg_match('/^[.\w-]+@([\w-]+\.)+[a-zA-Z]{2,6}$/', $string); } //Check cur page if($_POST['this_page'] == NULL){$_POST['this_page'] = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF'];} //If contact is being sent: if($_POST['submit_id'] == 1){ //Check name entered if($_POST['name'] == NULL){ $message = 'Please enter your name.';} //check if email is enetered if($message == NULL && $REQUIRE_MESS == 1 && $_POST['message_text'] == NULL){ $message = 'Please enter a comment.';} //check if message is entered if($message == NULL && $REQUIRE_MESS == 1 && $_POST['message_text'] == NULL){ $message = 'Please enter a comment.';} //check to send emails if($message == NULL) { if($MULTIPLE_EMAILS == 1){ //multiple emails $valid_emails = array(); $emails = explode(',',$_POST['toemails']); foreach($emails as $email){ $email = trim($email); if($email != NULL){ if(is_valid_email($email) == false ){ $temp_message .= '<br/>We couldn\'t send and email to: <font color="#0000FF">'.$email.'</font> because it is not valid.'; } else { array_push($valid_emails,$email); } } } if(count($valid_emails) <=0 ){ $message = 'Please enter at least one email to send message to.'; } } else { //one email if(is_valid_email($_POST['toemails']) == false ){ $message = 'Please enter a valid friend\'s email.';} } } //End verifications, start processing if($message == NULL){ //compose user message templates replaces $do_search = array('$+name+$','$+page_send+$','$+message_text+$'); $do_replace = array($_POST['name'],$_POST['this_page'],$_POST['message_text']); $subject = str_replace($do_search,$do_replace,$EMAIL_TEMPLATE); //Set Headers $headers = "Return-Path: ".$EMAIL_OPTIONS['TITLE']." <".$EMAIL_OPTIONS['FROM'].">\r\n"; $headers .= "From: ".$EMAIL_OPTIONS['TITLE']." <".$EMAIL_OPTIONS['FROM'].">\r\n"; $headers .= "Content-Type: ".$EMAIL_OPTIONS['TYPE']."; charset=".$EMAIL_OPTIONS['CHARSET'].";\n\n\r\n"; if($MULTIPLE_EMAILS == 1){ foreach($valid_emails as $this_email){ mail ($this_email,$EMAIL_OPTIONS['EMAIL_SUBJECT'],$subject,$headers); $one_pass = true; } } else { mail ($_POST['toemails'],$EMAIL_OPTIONS['EMAIL_SUBJECT'],$subject,$headers); } $message = 'Your emails have been sent, thank you.'; $message .= $temp_message; $_POST = NULL; } } if($message != NULL){ ?> Can i insert the message code on another page some how? -- not sure, I am way out of my depth here. Quote Link to comment https://forums.phpfreaks.com/topic/55276-clearing-post-data-browser-refresh-re-submits-email-form/#findComment-273279 Share on other sites More sharing options...
drewman Posted June 12, 2007 Author Share Posted June 12, 2007 Can we add some code to this that will check to see whether post data is duplicate and if so clear data. Am I on the right path here? Quote Link to comment https://forums.phpfreaks.com/topic/55276-clearing-post-data-browser-refresh-re-submits-email-form/#findComment-273327 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.