May 8, 200917 yr Hi. I really know little to nothing about PHP at all. My limit is to more or less understand one PHP mail script I have except for this bit which is annoying me. I have a contact form on my website and a php file to which the form gets posted to (i dont even know the correct terminologies here). This all works, however I'm modifying it for another form. It's nearly identical but on the new form i want 3 required fields and not 2 like in my old working form. Bellow, you'll see the code as it was which works and then the new code which doesn't work. It gives me the following error: "Parse error: syntax error, unexpected $end in mail.php on line 36". code that works with 2 required fields. <?php $to = "my@email.com"; $from = $_REQUEST['Email']; $name = $_REQUEST['Name']; $message = $_REQUEST['Message']; $headers = "From: $from"; $subject = "Website Enquiry"; $fields = array(); $fields{"Name"} = "Name"; $fields{"Email"} = "Email"; $fields{"Organization"} = "Organization"; $fields{"City"} = "City"; $fields{"Message"} = "Message"; $body = "Details submitted:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); } $headers2 = "From: my@email.com"; $subject2 = "Thank you for contacting us"; $autoreply = "Thank you for contacting us"; if($name == '') {print "You have not entered a name, please go back and try again.";} else { if($from == '') {print "You have not entered an email address, please go back and try again.";} else { $send = mail($to, $subject, $body, $headers); $send2 = mail($from, $subject2, $autoreply, $headers2); if($send) {header( "Location: http://www.website.com/sent.php" );} else {print "There was an error sending your request. Please try contacting us direct at my@email.com"; } } } ?> code that doesn't work with 3 required fields <?php $to = "my@email.com"; $from = $_REQUEST['Email']; $name = $_REQUEST['Name']; $message = $_REQUEST['Message']; $headers = "From: $from"; $subject = "Website Enquiry"; $fields = array(); $fields{"Name"} = "Name"; $fields{"Email"} = "Email"; $fields{"Organization"} = "Organization"; $fields{"City"} = "City"; $fields{"Message"} = "Message"; $body = "Details submitted:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); } $headers2 = "From: my@email.com"; $subject2 = "Thank you for contacting us"; $autoreply = "Thank you for contacting us"; if($message == '') {print "You have not entered any message.";} else { if($name == '') {print "You have not entered a name, please go back and try again.";} else { if($from == '') {print "You have not entered an email address, please go back and try again.";} else { $send = mail($to, $subject, $body, $headers); $send2 = mail($from, $subject2, $autoreply, $headers2); if($send) {header( "Location: http://www.website.com/sent.php" );} else {print "There was an error sending your request. Please try contacting us direct at my@email.com"; } } } ?> The only difference is that in the newer version, I've added: if($message == '') {print "You have not entered any message.";} else { ...and as someone who knows nothing about PHP, it has me stumped why this won't work. Surely it's an exact copy of the other two required fields validators that are there. Any help is greatly appreciated!
May 9, 200917 yr if ($message == '') { print "You have not entered any message."; } else { if ($name == '') { print "You have not entered a name, please go back and try again."; } else { if ($from == '') { print "You have not entered an email address, please go back and try again."; } else { $send = mail($to, $subject, $body, $headers); $send2 = mail($from, $subject2, $autoreply, $headers2); if ($send) { header("Location: [url="http://www.website.com/sent.php"%3b%29;"]http://www.website.com/sent.php");[/url] } else { print "There was an error sending your request. Please try contacting us direct at my@email.com"; } } } Above is your current code, as you can see with proper formatting it doesn't contain enough "}" closing brackets. I think the code below should fix your problem. if ($message == '') { print "You have not entered any message."; } else { if ($name == '') { print "You have not entered a name, please go back and try again."; } else { if ($from == '') { print "You have not entered an email address, please go back and try again."; } else { $send = mail($to, $subject, $body, $headers); $send2 = mail($from, $subject2, $autoreply, $headers2); if ($send) { header("Location: http://www.website.com/sent.php"); } else { print "There was an error sending your request. Please try contacting us direct at my@email.com"; } } } } Anyway, you're new to this so I'd recommend you always format your code properly it makes reading easier and allows for you to spot small mistakes better. You could always use if (!empty($message)) { instead of if ($message == '') { Anyway keep coding and have fun
May 9, 200917 yr Author if ($message == '') { print "You have not entered any message."; } else { if ($name == '') { print "You have not entered a name, please go back and try again."; } else { if ($from == '') { print "You have not entered an email address, please go back and try again."; } else { $send = mail($to, $subject, $body, $headers); $send2 = mail($from, $subject2, $autoreply, $headers2); if ($send) { header("Location: [url="http://www.website.com/sent.php"%3b%29;"]http://www.website.com/sent.php");[/url] } else { print "There was an error sending your request. Please try contacting us direct at my@email.com"; } } } Above is your current code, as you can see with proper formatting it doesn't contain enough "}" closing brackets. I think the code below should fix your problem. if ($message == '') { print "You have not entered any message."; } else { if ($name == '') { print "You have not entered a name, please go back and try again."; } else { if ($from == '') { print "You have not entered an email address, please go back and try again."; } else { $send = mail($to, $subject, $body, $headers); $send2 = mail($from, $subject2, $autoreply, $headers2); if ($send) { header("Location: http://www.website.com/sent.php"); } else { print "There was an error sending your request. Please try contacting us direct at my@email.com"; } } } } Anyway, you're new to this so I'd recommend you always format your code properly it makes reading easier and allows for you to spot small mistakes better. You could always use if (!empty($message)) { instead of if ($message == '') { Anyway keep coding and have fun Thanks very much, can't add this right now but will tomorrow. I'll let you know if it fixes it. Much appreciated!
Create an account or sign in to comment