Server side form validation in PHP

  • Tech Area
  • August 4, 2023



In this tutorial, We will learn how to implement server-side validation in HTML form using PHP. We will validate empty field, valid email id format and numeric value.

Server-side validation is when the user input is validated by the server after it is received from the browser.

Files used in this tutorial:

1- connection.php (database connection file)

2- index.php (registration form and server-side validation)

Below are the step by step process of how to implement server-side validation.

Step 1: Create a Database connection

In this step, create a new file connection.php to create database connection.

connection.php

<?php
$server = "localhost";
$username = "root";
$password = "";
$database = "college_db";
$connection = mysqli_connect("$server","$username","$password");
$select_db = mysqli_select_db($connection, $database);
if(!$select_db)
{
	echo("connection terminated");
}
?>

Step 2: Create Registration form

In this step, create a new file index.php. This is the main file used for server-side validation. First, we will create an HTML form with the fields i.e Name, Date of Birth, Email id and Mobile.

This screenshot shows the UI for Registration form.

index.php

<html>  
<head>  
    <title>Server Side Validation</title>  
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />  
</head>
<style>
 .box
 {
  width:100%;
  max-width:600px;
  background-color:#f9f9f9;
  border:1px solid #ccc;
  border-radius:5px;
  padding:16px;
  margin:0 auto;
 }
 .error
{
  color: red;
  font-weight: 700;
} 
</style>
<body>  
    <div class="container">  
    <div class="table-responsive">  
    <h3 align="center">Server Side Validation</h3><br/>
     <div class="box">
     <form id="validate_form" method="post" >
     <div class="form-group">
       <label for="name">Name</label>
       <input type="text" name="name" placeholder="Enter Name" class="form-control" value="<?php if(isset($name)){ echo $name; }?>"/>
       <span class="text-danger"><?php if(!empty($name_error)){ echo $name_error; } ?></span>
      </div> 
      <div class="form-group">
       <label for="dob">Date of Birth</label>
       <input type="date" name="dob" class="form-control" value="<?php if(isset($dob)){ echo $dob; }?>"/>
       <span class="text-danger"><?php if(!empty($dob_error)){ echo $dob_error; } ?></span>
      </div> 
       <div class="form-group">
       <label for="email">Email</label>
       <input type="text" name="email" placeholder="Enter Email" class="form-control" value="<?php if(isset($email)){ echo $email; }?>"/>
       <span class="text-danger"><?php if(!empty($email_error)){ echo $email_error; } ?></span>
      </div>
      <div class="form-group">
       <label for="mobile">Mobile No.</label>
       <input type="text" name="mobile" id="mob" placeholder="Enter Mobile" class="form-control" value="<?php if(isset($mobile)){ echo $mobile; }?>"/>
       <span class="text-danger"><?php if(!empty($mobile_error)){ echo $mobile_error; } ?></span>
      </div>
      <div class="form-group">
       <input type="submit" name="submit" value="Submit" class="btn btn-success" />
       </div>
       <p class="error"><?php if(!empty($msg)){ echo $msg; } ?></p>
     </form>
     </div>
   </div>  
  </div>
 </body>  
</html>

Now validate user input fields and submit the data in the table tbl_student.

<?php
$error=0;
include('connection.php');
if(isset($_REQUEST['submit']))
{
  $name = $_REQUEST['name'];
  $dob = $_REQUEST['dob'];
  $email = $_REQUEST['email'];
  $mobile = $_REQUEST['mobile'];

  if(empty($name))
  {
    $name_error = "Please enter the Name";
    $error=1;
  }
  else if(!preg_match("/^[a-zA-Z ]*$/", $name))
  {
    $name_error = "Only letters are allowed";
    $error=1;
  }
  if(empty($dob))
  {
    $dob_error = "Please enter the Date of Birth";
    $error=1;
  }
  if(empty($email))
  {
    $email_error = "Please enter the Email Id";
    $error=1;
  }
  else if(!filter_var($email, FILTER_VALIDATE_EMAIL))
  {
    $email_error = "Invalid Email Format";
    $error=1;
  }
  if(empty($mobile))
  {
    $mobile_error = "Please enter the Mobile Number";
    $error=1;
  }

  if($error==0)
  {
  $insert_query = mysqli_query($connection,"insert into tbl_student set name='$name', dob='$dob', email='$email', mobile='$mobile'");
  if($insert_query>0)
  {
    $msg = "Registration successfull";
  }
  else
  {
    $msg = "Error!";
  }
}
else
{
  $msg = "Please fill all fields";
}
}
?>

Source Code

Here is the full code that we have written for index.php.

<html>  
<head>  
    <title>Server Side Validation</title>  
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />  
</head>
<style>
 .box
 {
  width:100%;
  max-width:600px;
  background-color:#f9f9f9;
  border:1px solid #ccc;
  border-radius:5px;
  padding:16px;
  margin:0 auto;
 }
 .error
{
  color: red;
  font-weight: 700;
} 
</style>
<?php
$error=0;
include('connection.php');
if(isset($_REQUEST['submit']))
{
  $name = $_REQUEST['name'];
  $dob = $_REQUEST['dob'];
  $email = $_REQUEST['email'];
  $mobile = $_REQUEST['mobile'];

  if(empty($name))
  {
    $name_error = "Please enter the Name";
    $error=1;
  }
  else if(!preg_match("/^[a-zA-Z ]*$/", $name))
  {
    $name_error = "Only letters are allowed";
    $error=1;
  }
  if(empty($dob))
  {
    $dob_error = "Please enter the Date of Birth";
    $error=1;
  }
  if(empty($email))
  {
    $email_error = "Please enter the Email Id";
    $error=1;
  }
  else if(!filter_var($email, FILTER_VALIDATE_EMAIL))
  {
    $email_error = "Invalid Email Format";
    $error=1;
  }
  if(empty($mobile))
  {
    $mobile_error = "Please enter the Mobile Number";
    $error=1;
  }

  if($error==0)
  {
  $insert_query = mysqli_query($connection,"insert into tbl_student set name='$name', dob='$dob', email='$email', mobile='$mobile'");
  if($insert_query>0)
  {
    $msg = "Registration successfull";
  }
  else
  {
    $msg = "Error!";
  }
}
else
{
  $msg = "Please fill all fields";
}
}
?>
<body>  
    <div class="container">  
    <div class="table-responsive">  
    <h3 align="center">Server Side Validation</h3><br/>
     <div class="box">
     <form id="validate_form" method="post" >
     <div class="form-group">
       <label for="name">Name</label>
       <input type="text" name="name" placeholder="Enter Name" class="form-control" value="<?php if(isset($name)){ echo $name; }?>"/>
       <span class="text-danger"><?php if(!empty($name_error)){ echo $name_error; } ?></span>
      </div> 
      <div class="form-group">
       <label for="dob">Date of Birth</label>
       <input type="date" name="dob" class="form-control" value="<?php if(isset($dob)){ echo $dob; }?>"/>
       <span class="text-danger"><?php if(!empty($dob_error)){ echo $dob_error; } ?></span>
      </div> 
       <div class="form-group">
       <label for="email">Email</label>
       <input type="text" name="email" placeholder="Enter Email" class="form-control" value="<?php if(isset($email)){ echo $email; }?>"/>
       <span class="text-danger"><?php if(!empty($email_error)){ echo $email_error; } ?></span>
      </div>
      <div class="form-group">
       <label for="mobile">Mobile No.</label>
       <input type="text" name="mobile" id="mob" placeholder="Enter Mobile" class="form-control" value="<?php if(isset($mobile)){ echo $mobile; }?>"/>
       <span class="text-danger"><?php if(!empty($mobile_error)){ echo $mobile_error; } ?></span>
      </div>
      <div class="form-group">
       <input type="submit" name="submit" value="Submit" class="btn btn-success" />
       </div>
       <p class="error"><?php if(!empty($msg)){ echo $msg; } ?></p>
     </form>
     </div>
   </div>  
  </div>
 </body>  
</html>

Download Source Code


Subscribe us via Email

Join 10,000+ subscriber

Subscribe on YouTube