Jump to content

Problems with If statement when sending Mail


richnb1974

Recommended Posts

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

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]";
}
?>

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.