jQuery Form Validation in PHP

jQuery Form Validation in PHP

jQuery form validation (Client-side validation) in PHP; In this tutorial; you will learn how to use jQuery validation or client side validation with PHP Forms using jQuery validation library.

This tutorial will create simple contact us form and add client-side validation on it in PHP MySQL using jQuery client-side validation library.

Simple jQuery (Client Side ) Form Validation in PHP + MySQL

Use the following steps to to implement client-side jquery validation in PHP + MySQL; as follows:

  • Step 1 – Create PHP Project
  • Step 2 – Create Table in Database
  • Step 3 – Create a Database Connection File
  • Step 4 – Create HTML Form
  • Step 5 – Add Client Side Validation on Form

Step 1 – Create PHP Project

First of all; visit your web server directory and create a php directory; which name demo.

Step 2 – Create Table in Database

Create table into your database; so visit your PHPMyAdmin and create a table name contacts_list with 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 3 – Create a Database Connection File

Create a file name db.php and update the below code into your file.

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

The above code is used to create a MySQL database connection in PHP. When we insert form data into MySQL database, there we will include this file:

Step 4 – Create HTML Form

Create a simple HTML contact us form and add the following code into your contact-form.php file:

<!doctype html>
<meta charset="utf-8">
<title>Contact Form in PHP</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
.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"];
// 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())");
<!-- 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">

Step 5 – Add Client Side Validation on Form

Add client-side validation on PHP Html forms; so add the jQuery client-side validation library and implement jQuery validation code with PHP forms; as shown below:

<!-- JavaScript -->
script src = "https://code.jquery.com/jquery-3.5.1.slim.min.js" > < /script> <
script src = "https://cdn.jsdelivr.net/npm/jquery-validation@1.19.2/dist/jquery.validate.min.js" > < /script>
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) {
}); <


Client-side validation in PHP MySQL using jQuery validation; In this tutorial; you have learned how to add client-side validation with PHP Forms using the jQuery validation library.

Leave a Reply

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