richnb1974 Posted July 23, 2010 Share Posted July 23, 2010 Hi guys, Please help me sort this annoying problem. Im new to the forum and quite new to Php.. weren't we all? My problem is, i have this code set up to send a form. However, when the form is completed and submitted it returns the error message i have written saying no email has been completed. The funny thing is though...it sends the email. this is my code: <?php $to = $_REQUEST['sendto'] ; $from = $_REQUEST['Email'] ; $name = $_REQUEST['Name'] ; $headers = "From: $from"; $subject = "Booking Request"; $fields = array(); $fields{"Name"} = "Name"; $fields{"Address"} = "Address"; $fields{"District"} = "District"; $fields{"Town"} = "Town"; $fields{"Postcode"} = "Postcode"; $fields{"Phone"} = "Phone"; $fields{"Email"} = "Email"; $fields{"list"} = "Mailing List"; $fields{"Message"} = "Message"; $body = "We have received the following information:\n\n"; foreach($fields as $a => $b) { $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); } $headers2 = "From: [email protected]"; $subject2 = "Thank you for contacting us"; if($from == '') {echo "You have not entered an email, please go back and try again";} else { if($name == '') {echo "You have not entered your name, please go back and try again";} else { $send = mail($to, $subject, $body, $headers); if($send) {header( "Location: http://www.21stcenturyportraits.com/emailresponse.php" );} else {print "We encountered an error sending your mail, please notify [email protected]"; } } } ?> As I said, the problem seems to be with my If statements, even though I enter an email address it shows my error message. Regards Rich Link to comment https://forums.phpfreaks.com/topic/208659-problems-with-if-statement-when-sending-mail/ Share on other sites More sharing options...
Pikachu2000 Posted July 23, 2010 Share Posted July 23, 2010 There's really nothing stopping the script if there's an error, or if the redirect occurs. Try this. The errors are stored in an array, and the mail won't send unless the errors array is empty. Also added the exit() after the header() redirect. I haven't tested it, but there shouldn't be any errors. <?php $to = $_REQUEST['sendto'] ; $from = $_REQUEST['Email'] ; $name = $_REQUEST['Name'] ; $headers = "From: $from"; $subject = "Booking Request"; $fields = array(); $fields{"Name"} = "Name"; $fields{"Address"} = "Address"; $fields{"District"} = "District"; $fields{"Town"} = "Town"; $fields{"Postcode"} = "Postcode"; $fields{"Phone"} = "Phone"; $fields{"Email"} = "Email"; $fields{"list"} = "Mailing List"; $fields{"Message"} = "Message"; $body = "We have received the following information:\n\n"; foreach($fields as $a => $b) { $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); } $headers2 = "From: [email protected]"; $subject2 = "Thank you for contacting us"; $errors = array(); if($from == '') { $errors[] = "You have not entered an email, please go back and try again"; } if($name == '') { $errors[] = "You have not entered your name, please go back and try again"; } if( !empty($errors) ) { foreach( $errors as $v ) { echo $v . "<br />"; } } elseif( mail($to, $subject, $body, $headers) ) { header( "Location: http://www.21stcenturyportraits.com/emailresponse.php" ); exit(); } else { print "We encountered an error sending your mail, please notify [email protected]"; } ?> Link to comment https://forums.phpfreaks.com/topic/208659-problems-with-if-statement-when-sending-mail/#findComment-1090146 Share on other sites More sharing options...
Pikachu2000 Posted July 23, 2010 Share Posted July 23, 2010 Double post . . . deleted. Link to comment https://forums.phpfreaks.com/topic/208659-problems-with-if-statement-when-sending-mail/#findComment-1090153 Share on other sites More sharing options...
richnb1974 Posted July 26, 2010 Author Share Posted July 26, 2010 Hi Pikachu, Thanks for the response. Unfortunately it is still doing the same thing and echoing both of the error messages after the email has been sent. I receive the emails perfectly well, even though there are error messages. It definitely seems to be the if($from =='') statement which isn't functionong properly. Thanks anyway. Any other suggestions are welcomed. Rich Link to comment https://forums.phpfreaks.com/topic/208659-problems-with-if-statement-when-sending-mail/#findComment-1091159 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.