CodeIn House

  • Laravel
  • WordPress
  • jQuery
  • Javascript
  • Contact
Home   PHP   PHP Contact Form Validation With Simple Example

PHP Contact Form Validation With Simple Example

June 19, 2021 by SNK

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 ?

  1. Create a simple contact form using bootstrap.
  2. Validate contact form using PHP
  3. 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.

Contact 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
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

PHP Form
PHP
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.

SHARE ON
Buffer

Enjoyed this article?

Like us on

PHP

Avatar for SNK

About SNK

Hello Welcome to my Blog. I develop Websites Using Laravel Framwork & WordPress. Get Latest updates on Facebook | Twitter

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Get Connected !! With Us

TOP POSTS

  • How to Setup Spring MVC Project From Scratch in Intellij IDEA ?
  • Spring Configuration Class in JAVA [Class VS XML Config]
  • Annotations Based Configuration in Spring Framework
  • How to Configure Spring Framework with XML Configurations?
  • How to Remove Project Name from URL in JSP Project in Intellij IDEA ?

TUTORIALS TILL DATE

  • September 2022 (6)
  • June 2021 (7)
  • October 2020 (5)
  • September 2020 (6)
  • September 2018 (14)
  • August 2018 (3)
  • July 2018 (4)
  • March 2018 (8)
  • February 2018 (5)
  • January 2018 (1)
  • December 2017 (2)
  • August 2017 (8)
  • May 2017 (1)
  • April 2017 (1)
  • March 2017 (4)
  • February 2017 (3)
  • January 2017 (4)

CATEGORIES

  • Angular (2)
  • CSS3 (3)
  • D3 (3)
  • HTML5 (7)
  • JAVA (11)
  • Javascript (20)
  • jQuery (8)
  • Laravel (35)
  • Others (3)
  • PHP (11)
  • Spring (2)
  • WordPress (10)

Top Categories

  • Angular
  • CSS3
  • D3
  • HTML5
  • JAVA
  • Javascript
  • jQuery
  • Laravel
  • Others
  • PHP
  • Spring
  • WordPress

Get in Touch

DMCA.com Protection Status

Recent Articles

  • How to Setup Spring MVC Project From Scratch in Intellij IDEA ?
  • Spring Configuration Class in JAVA [Class VS XML Config]
  • Annotations Based Configuration in Spring Framework
  • How to Configure Spring Framework with XML Configurations?
  • How to Remove Project Name from URL in JSP Project in Intellij IDEA ?

© 2012-22 CodeIn House.  •  All Rights Reserved.