Making the Fields in a Contact Form required
<?php
if(isset($_POST['submit'])) {
$myemail = "[email protected]";
$subject = $_POST['subject'];
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$headers = "From:Contact Form <$myemail>\r\n";
$headers .= "Reply-To: $name <$email>\r\n";
echo "Your message has been sent successfully!";
mail($myemail, $subject, $message, $headers);
} else {
echo "An error occurred during the submission of your message";
}
?>
Anyone have any suggestions on how to make the above fields required in the contact form?
Thanks.
Greg K posted this at 08:52 — 17th February 2019.
He has: 2,145 posts
Joined: Nov 2003
Just check to see if the variables are blank.
<?php
$bSubmitted = false;
$errors = [];
$bHasErrors = false;
if ( isset( $_POST['submit'] ) ) {
$bSubmitted = true;
$subject = trim( $_POST['subject'] );
$name = trim( $_POST['name'] );
$email = trim( $_POST['email'] );
$message = trim( $_POST['message'] );
if ( trim( $subject ) == '' ) {
$errors['subject'] = 'Subject is required';
}
if ( trim( $name] ) == '' ) {
$errors['name'] = 'Name is required';
}
if ( trim( $email ) == '' ) {
$errors['email'] = 'Email is required';
} elseif ( filter_var( $email, FILTER_VALIDATE_EMAIL ) ) {
$errors['email'] = 'Invalid E-mail address';
}
if ( trim( $message ) == '' ) {
$errors['message'] = 'Message is required';
}
$bHasErrors = ( count( $errors ) > 0 );
if ( ! $bHasErrors ) {
$myemail = "[email protected]";
$headers = "From:Contact Form <$myemail>\r\n";
$headers .= "Reply-To: $name <$email>\r\n";
if ( ! mail( $myemail, $subject, $message, $headers ) ) {
$errors['send'] = 'There was a problem sending the message.';
$bHasErrors = true;
}
}
} else {
// Set the variables to blank since they will be used in the form
$subject = '';
$name = '';
$email = '';
$message = '';
}
?>
Now on the page that displays, where you have the form, you can do: (sorry, this forum screws up trying to show HTML code with PHP code, and got rid of indentation....)
<?php if ( $bSubmitted && ! $bHasErrors ) { ?>
FORM SENT MESSAGE HERE...
<?php } else { ?>
FORM WASN'T SENT (or first visit) SO SHOW THE FORM
TELL THEM THEY HAVE ERRORS IF THEY DO, One way, put it in a list...
<?php if ( $bHasErrors ) {
echo "<p>You had errors!</p>";
echo '<ul><li>', implode('</li><li>', $errors ), '</li></ul>';
} ?>
<form method="post" action="">
SUBJECT: <input type="text" name="subject" value="<?= htmlspecialchars($subject) ?>"><br>
NAME: <input type="text" name="name" value="<?= htmlspecialchars($name) ?>"><br>
EMAIL: <input type="text" name="email" value="<?= htmlspecialchars($email) ?>"><br>
MESSAGE: <textarea name="message"><?= htmlspecialchars($message) ?></textarea><br>
<input type="submit" name="submit" value="Send Message">
</form>
<?php } ?>
Alternatively, instead of listing all errors at the top, since the errors are keyed for the inputs you could do something like:
<form method="post" action="">
<?php if (array_key_exists('subject',$errors) ) { echo $errors['subject']; } ?>
SUBJECT: <input type="text" name="subject" value="<?= htmlspecialchars($subject) ?>"><br>
<?php if (array_key_exists('name',$errors) ) { echo $errors['name']; } ?>
NAME: <input type="text" name="name" value="<?= htmlspecialchars($name) ?>"><br>
<?php if (array_key_exists('email',$errors) ) { echo $errors['email']; } ?>
EMAIL: <input type="text" name="email" value="<?= htmlspecialchars($email) ?>"><br>
<?php if (array_key_exists('message',$errors) ) { echo $errors['message']; } ?>
MESSAGE: <textarea name="message"><?= htmlspecialchars($message) ?></textarea><br>
<input type="submit" name="submit" value="Send Message">
</form>
As you can see, if you have a lot of inputs, you'd want to just put that into a function...
The benefit of this method is that when they do have errors, they do not have to start over with a fresh blank form, what they already submitted will be filled into the form when it shows again, with either the errors listed at the top or above each input.
dansbanners posted this at 19:56 — 19th February 2019.
They have: 4 posts
Joined: Apr 2008
FYI. Using the codes above, the submitted form leads to a blank .php page. Any idea on what could be causing it? Thanks again!
Greg K posted this at 06:04 — 25th February 2019.
He has: 2,145 posts
Joined: Nov 2003
I may not have been clear, all the code should be on the same .php file and then the form should be submitting back to itself, so if it is, and you are not getting anything, check the error logs for possible errors.
I'm home now, so threw the code in an IDE, and see that I accidently had a typo on this line:
if ( trim( $name ) == '' ) {
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.