In this tutorial we are going to see PHP contact form validation with simple example. I believe this is the most simple form validation in PHP that I have come across.
We are also going to implement simple verification for it so, that you will not get any type of spams and advertising messages in your inbox if you implement this in your website.
let’s get started
What are we going to do ?
- Create a simple contact form using bootstrap.
- Validate contact form using PHP
- Sending an HTML email
PHP Contact Form Validation With Simple Example
To validate the form using PHP first we have to create a form and submit it and validate it fields in the form are empty or not and then after check if user’s have entered correct email etc.
So let’s start by creating a form first and as usual I am going to use bootstrap framework for this. Let’s Create the template.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
<html> <head> <title>PHP Contact From Validation - CodeInHouse.com</title> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/css/bootstrap.min.css"> </head> <body> <div class="container mt-5"> <div class="row"> <div class="col-md-8 m-auto"> <h1>Simple Contact Form Validation Using PHP</h1> <small class="mb-3">Simplest Form Validation by <a href="http://www.codeinhouse.com">codeinhouse.com</a></small> <?php foreach ($message as $value): ?> <?php if (count($value) > 0): ?> <div class="alert alert-danger alert-dismissible"> <?php foreach ($value as $v): ?> <?php echo $v; ?> <?php endforeach; ?> </div> <?php endif; ?> <?php endforeach; ?> <form action="#" method="POST"> <div class="mb-3"> <label for="full_name" class="form-label">Full Name</label> <div class="col-6"> <input type="text" name="full_name" id="full_name" class="form-control"> </div> </div> <div class="mb-3"> <label for="full_name" class="form-label">Email</label> <div class="col-6"> <input type="text" name="email" id="email" class="form-control"> </div> </div> <div class="mb-3"> <label for="full_name" class="form-label">Message</label> <div class="col-12"> <textarea name="message" cols="30" rows="5" class="form-control"></textarea> </div> </div> <div class="mb-3"> <label for="full_name" class="form-label">Verify yourself</label> <div class="row"> <div class="col-2"> <input type="number" name="num1" id="num1" value="<?php echo rand(1, 10); ?>" class="form-control" readonly> </div> + <div class="col-2"> <input type="number" name="num2" id="num2" class="form-control" value="<?php echo rand(1, 10); ?>" readonly> </div> = <div class="col-2"> <input type="number" name="result" id="result" class="form-control"> </div> </div> </div> <div class="mb-3"> <div class="row"> <div class="col-2"> <button type="submit" class="btn btn-primary">Submit</button> </div> </div> </div> </form> </div> </div> </div> </body> </html> |
PHP code to submit the form and validate. Make sure to copy and paste this code on top of the html form
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
<?php $message = [ 'full_name' => [], 'email' => [], 'message' => [], 'validation' => [] ]; if ($_SERVER['REQUEST_METHOD'] === 'POST') { // check if user has passed the verification if (isset($_POST['num1']) && isset($_POST['num2']) && isset($_POST['result'])) { $num1 = (int) $_POST['num1']; $num2 = (int) $_POST['num2']; $result = (int) $_POST['result']; echo $result; if (($num1 + $num2) === $result) { // If verification is success then check if the verification is passed if (empty($_POST['full_name'])) { array_push($message['full_name'], 'Full name is empty'); } // Validating email if (empty($_POST['email'])) { array_push($message['email'], 'Email is empty'); } if (!empty($_POST['email']) && !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) { array_push($message['email'], 'Invalid email address.'); } echo $_POST['message']; if (empty($_POST['message'])) { array_push($message['message'], 'Message is empty'); } } else { array_push($message['validation'], 'Could not pass verification'); } } } ?> |
That is it for PHP contact form validation with example. This validation can validate each field with different type of error messages. Of course there are many other methods and ideas but for beginning it would be fine to use it
.If you want to ask anything make sure to leave it there on comments.
Leave a Reply