dirtychinchilla Posted February 13, 2011 Share Posted February 13, 2011 Hi guys, this is my first post here. I'm looking for some help with some code for a form submission. It all works fine but if I add in a redirect using the advice found here: http://www.computing.net/answers/webdevel/redirection-after-submit-php-form/3580.html. I'm using the following code: <?php header ('Location: http://www.jwhunterhill.co.uk/return.html'); exit (); if(isset($_POST['email'])) { // EDIT THE 2 LINES BELOW AS REQUIRED $email_to = "xxx@gmail.com"; $email_subject = "E-mail from JWHunterHill.co.uk"; function died($error) { // your error code can go here echo "We are very sorry, but there were error(s) found with the form you submitted. "; echo "These errors appear below.<br /><br />"; echo $error."<br /><br />"; echo "Please go back and fix these errors.<br /><br />"; die(); } // validation expected data exists if(!isset($_POST['first_name']) || !isset($_POST['last_name']) || !isset($_POST['email']) || !isset($_POST['telephone']) || !isset($_POST['comments'])) { died('We are sorry, but there appears to be a problem with the form you submitted.'); } $first_name = $_POST['first_name']; // required $last_name = $_POST['last_name']; // required $email_from = $_POST['email']; // required $telephone = $_POST['telephone']; // not required $comments = $_POST['comments']; // required $error_message = ""; $email_exp = "^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$"; if(!eregi($email_exp,$email_from)) { $error_message .= 'The Email Address you entered does not appear to be valid.<br />'; } $string_exp = "^[a-z .'-]+$"; if(!eregi($string_exp,$first_name)) { $error_message .= 'The First Name you entered does not appear to be valid.<br />'; } if(!eregi($string_exp,$last_name)) { $error_message .= 'The Last Name you entered does not appear to be valid.<br />'; } if(strlen($comments) < 2) { $error_message .= 'The Comments you entered do not appear to be valid.<br />'; } if(strlen($error_message) > 0) { died($error_message); } $email_message = "Form details below.\n\n"; function clean_string($string) { $bad = array("content-type","bcc:","to:","cc:","href"); return str_replace($bad,"",$string); } $email_message .= "First Name: ".clean_string($first_name)."\n"; $email_message .= "Last Name: ".clean_string($last_name)."\n"; $email_message .= "Email: ".clean_string($email_from)."\n"; $email_message .= "Telephone: ".clean_string($telephone)."\n"; $email_message .= "Comments: ".clean_string($comments)."\n"; // create email headers $headers = 'From: '.$email_from."\r\n". 'Reply-To: '.$email_from."\r\n" . 'X-Mailer: PHP/' . phpversion(); @mail($email_to, $email_subject, $email_message, $headers); ?> <!-- include your own success html here --> Thank you for contacting me. I will be in touch with you very soon. <?php } ?> As I said the code works fine just if I include the redirect, the form will not send me an e-mail as required. Could you help me out please? Thanks in advance, Jonathon Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 13, 2011 Share Posted February 13, 2011 1. you're redirecting before you do anything. If you want things to happen before you redirect, you need to rearrange your code. 2. Don't suppress errors (@) during development. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted February 13, 2011 Share Posted February 13, 2011 Well. of course it won't. What do you think using header() and exit() does to the script? Quote Link to comment Share on other sites More sharing options...
dirtychinchilla Posted February 13, 2011 Author Share Posted February 13, 2011 Thank you for your advice. I know very little about PHP and as a consequence my editing is a little vague. I tried moving the header to the bottom but it prevented the whole script from working. With regards to using @, I wasn't aware that it did that... Quote Link to comment 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.