I have set up the following form to send mail from a website I am working on. I have set it to redirect to a thank you page if it sends and also an error page (which I have designed) if it fails.
However, it isn't working with the code below, I just get a blank page. If I remove the
if (empty... parts it will send fine, but then I don't have error message if all the fields haven't been filled in.
Here is the code:
<?php
if(isset($_POST['submit'])) {
if (empty($_POST['name']) || empty($_POST['phone']) || empty($_POST['email']) || empty($_POST['message']) ){
header( "Location: form-error.php");
}
}
$to = "me@website.ie";
$subject = "Email from Website";
$name_field = $_POST['name'];
$phone_field = $_POST['phone'];
$email_field = $_POST['email'];
$message = $_POST['message'];
foreach($_POST['check'] as $value) {
$check_msg .= "\n$value\n";
}
$body = "Name: $name_field\n\n E-Mail: $email_field\n\n Phone: $phone_field\n\n Message:\n\n $message\n\n";
$header="From: $email_field\r\n";
mail($to, $subject, $body,$header);
header("Location: thanks.php");
} else {
echo "!";
}