php contact form with jquery validation


Simply contact us form submit in PHP, MySQL with jQuery validation. In this article, you will see how to create a contact form using PHP, and submit the contact us form record in MySQL DB using jQuery validation.

This article helps you to create and submit contact forms with PHP and MySQL DB using jQuery validation. And you can also use this form for your website queries. The best thing is you can use this form in your web App too.

And, you can use the free code of PHP contact us form with the DB source code.

PHP Contact us Form in PHP using jquery Validation and Database

Just follow the few below instruction and easily generate and submit contact form with PHP MySQL using jquery validation.

  • Step 1 – Create Table in Database
  • Step 2 – Create a Database Connection File
  • Step 3 – Create Contact Form in PHP

Step 1 – Create Table in Database

First, Open your PHPMyAdmin and create a table with name contacts_list using the following fields: name, email, mobile.

CREATE TABLE `contacts_list` (
  `name` varchar(100) NOT NULL,
  `email` varchar(100) NOT NULL,
  `phone` varchar(50) NOT NULL,
  `subject` varchar(255) NOT NULL,
  `Message` text NOT NULL,
  `sent_date` datetime NOT NULL

Step 2 – Create a Database Connection File

Now, you need to create a file name db.php and update the below given code into your php file.

The below given code is used to create a MySQL database connection in PHP. When we go insert form record into MySQL DB, there we will include this file:

    $dbname = "my_db";
          die('Could not Connect MySql Server:' .mysql_error());

Step 3 – Create Contact Form in PHP

Next Step, you will create HTML contact form and add following below given code into your contact-form.php file:

<!doctype html>
<meta charset="utf-8">
<title>Contact Form in PHP</title>
<link rel="stylesheet" href="">
.container {
max-width: 500px;
margin: 50px auto;
text-align: left;
font-family: sans-serif;
form {
border: 1px solid #1A33FF;
background: #ecf5fc;
padding: 40px 50px 45px;
.form-control:focus {
border-color: #000;
box-shadow: none;
label {
font-weight: 600;
.error {
color: red;
font-weight: 400;
display: block;
padding: 6px 0;
font-size: 14px;
.form-control.error {
border-color: red;
padding: .375rem .75rem;
<div class="container mt-5">
if(!empty($_POST["send"])) {
$name = $_POST["name"];
$email = $_POST["email"];
$phone = $_POST["phone"];
$subject = $_POST["subject"];
$message = $_POST["message"];
// Recipient email
$toMail = "";
// Build email header
$header = "From: " . $name . "<". $email .">\r\n";
// Send email
if(mail($toMail, $subject, $message, $header)) {
// Store contactor data in database
$sql = $connection->query("INSERT INTO contacts_list(name, email, phone, subject, message, sent_date)
VALUES ('{$name}', '{$email}', '{$phone}', '{$subject}', '{$message}', now())");
if(!$sql) {
die("MySQL query failed.");
} else {
$response = array(
"status" => "alert-success",
"message" => "We have received your query and stored your information. We will contact you shortly."
} else {
$response = array(
"status" => "alert-danger",
"message" => "Message coudn't be sent, try again"
<!-- Messge -->
<?php if(!empty($response)) {?>
<div class="alert text-center <?php echo $response['status']; ?>" role="alert">
<?php echo $response['message']; ?>
<?php }?>
<!-- Contact form -->
<form action="" name="contactForm" method="post" enctype="multipart/form-data">
<div class="form-group">
<input type="text" class="form-control" name="name" id="name">
<div class="form-group">
<input type="email" class="form-control" name="email" id="email">
<div class="form-group">
<input type="text" class="form-control" name="phone" id="phone">
<div class="form-group">
<input type="text" class="form-control" name="subject" id="subject">
<div class="form-group">
<textarea class="form-control" name="message" id="message" rows="4"></textarea>
<input type="submit" name="send" value="Send" class="btn btn-dark btn-block">
<!-- JavaScript -->
<script src=""></script>
<script src=""></script>
$(function() {
// Define validation rules
rules: {
name: "required",
email: "required",
phone: "required",
subject: "required",
message: "required",
name: {
required: true
email: {
required: true,
email: true
phone: {
required: true,
minlength: 10,
maxlength: 10,
number: true
subject: {
required: true
message: {
required: true
// Specify validation error messages
messages: {
name: "Please provide a valid name.",
email: {
required: "Please enter your email",
minlength: "Please enter a valid email address"
phone: {
required: "Please provide a phone number",
minlength: "Phone number must be min 10 characters long",
maxlength: "Phone number must not be more than 10 characters long"
subject: "Please enter subject",
message: "Please enter your message"
submitHandler: function(form) {


In this article, you have learned how to generate contact form in php with using jQuery validation and add data into a MySQL database.

Recommended PHP Tutorials

Like it? Share with your friends!