PHP Contact US Form with Validation Tutorial

PHP contact us form with database and validation; In this tutorial, i will show you how to make simple php contact us form and storing form data into mysql database with validation.

As well as you can use this php contact form with validation free download.

In this tutorial, i will create database for store/save contact us form data. Then will create database connecting file and contact us form in Html. After that, will create store contact us form data and thank html page.

PHP MySQL Contact us Form with Validation

Let’s follow the below given steps to make contact form script with validation in PHP MySQL:

  • Step 1 – Create PHP Project
  • Step 2 – Create phpmyadmin MySQL Database and Connecting File
  • Step 3 – Create Contact Us Form In HTML
  • Step 4 – Store Contact Us Form to Database
  • Step 5 – Create Thank You Html Page

Step 1 – Create PHP Project

Navigate to your local web server directory. And inside this directory, create one directory. And you can name this folder anything.

Here, I will “demo” the name of this folder. Then open this folder in any text editor (i will use sublime text editor).

Step 2 – Create phpmyadmin MySQL Database and Connecting File

In step 2, you need to create database and table. So run the following sql query to create database and table:

CREATE DATABASE my_db;


CREATE TABLE `contact_us` (
  `id` int(10) UNSIGNED NOT NULL,
  `name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `email` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `message` text COLLATE utf8mb4_unicode_ci NOT NULL,
  `created_date` date DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

Then create a php file that named mydbCon. Which is used to connect phpmyadmin mysql database to project (demo).

So, now create mydbCon file and add the below given code into your file:

<?php

    $hName='localhost'; // host name

    $uName='root';   // database user name

    $password='';   // database password

    $dbName = "my_db"; // database name

    $dbCon = mysqli_connect($hName,$uName,$password,"$dbName");

      if(!$dbCon){
          die('Could not Connect MySql Server:' .mysql_error());
      }
?>

Step 3 – Create Contact Us Form In HTML

In step 3, create a php file that named contact-us.php. This file will display PHP MySQL contact us form and store form data into MySQL database with validation.

Here, i will use html validation with PHP MySQL contact us form. Now, you need to add the following contact us form code into contact-us.php file:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>PHP Contact US Form with Database and Validation</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>

<div class="container mt-2">
    <div class="row">
        <div class="col-md-12">
            <div class="page-header">
                <h2>PHP MySQL Contact US Form with Database</h2>
            </div>
           
            <form action="store-contact-us.php" method="post">

                <div class="form-group">
                    <label>Name</label>
                    <input type="text" name="name" class="form-control" required="">
                </div>                        


                <div class="form-group">
                    <label>Email</label>
                    <input type="email" name="email" class="form-control" required="">
                </div>

                <div class="form-group">
                    <label>Message</label>
                    <input type="text" name="massage" class="form-control" required="">
                </div>

                <input type="submit" class="btn btn-primary" name="submit" value="submit">
            </form>
        </div>
    </div>        
</div>

</body>
</html>

Step 4 – Store Contact Us Form to Database

In step 4, create new file that named store-contact-us.php file. Because this is used to store contact us form data into mysql database.

So, add the below code into your store-contact-us.php file:

<?php

if(count($_POST)>0)
{    
     include 'mydbCon.php';

     $name = $_POST['name'];
     $message = $_POST['message'];
     $email = $_POST['email'];
 
     $query = "INSERT INTO contact_us (name,message,email)
     VALUES ('$name','$message','$email')";

     mysqli_query($dbCon, $query);

     $lastId = mysqli_insert_id($dbCon);
 
     if (!empty($lastId)) {
        $message = "Your contact information is saved successfully";
     }

}
  header ("Location: thank-you.php");
?>

Step 5 – Create Thank You Html Page

In step 5, create thank-you.php file. After successfully saved contact us form data in PHP MySQL database. Then i will redirect to thank you page.

Add the following code into thank-you.php file:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Thanks for Contact US - PHP MySQL</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>

<div class="container mt-2">
    <div class="row">
        <div class="col-md-12">
            <div class="page-header">
                <h2>Thanks for contacting us. We will get back you.</h2>
            </div>
        </div>
    </div>        
</div>

</body>
</html>

Conclusion

In this tutorial, i will show you how to make simple php contact us form and storing form data into mysql database with validation.

Recommended PHP Tutorials