PHP

Server Side Form Validation in PHP.

Server Side Form Validation in PHP.

Validation may be a quite common task in web applications. Entered data should be Validated before sending it to the server. This post deals with server-side form validation in PHP. After reading this post we’ll be ready to validate the form with PHP.

The server-side form validation is more secure than the client-side form validation. Client-side validation depends on whether the JavaScript is disabled or enabled on the client system. If JavaScript is disabled then client-side form validation will not work. However server-side for validation done on server-side after the data has been submitted.

Step 1: Create index.php file and add the below line of codes.nnnnn

In this file, we have created a basic registration page containing Name, Email, Mobile Number, Password, and Confirm Password. After submitting the form validate.php will validate the form whether the submitted data is as per our requirement or not. If all the submitted data is as per our requirement then we can process it for further use otherwise error message will be displayed and the user redirects back to the home page.

<?php
session_start(); 
$nameError  = isset($_SESSION['nameError'])?$_SESSION['nameError']:'';
$emailError  = isset($_SESSION['emailError'])?$_SESSION['emailError']:'';
$mobnoError  = isset($_SESSION['mobnoError'])?$_SESSION['mobnoError']:'';
$passwordError  = isset($_SESSION['passwordError'])?$_SESSION['passwordError']:''; 
?>
<html lang="en">
<head>c 
  <title>Server Side Form Validation In PHP</title>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">  
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"> 
<body>
<h3 class="text-success" align="center">Server Side Form Validation In PHP</h3><br>
<div class="container">   
  <div class="panel-group">
    <div class="panel panel-primary">
     <div class="panel-heading">Server Side Form Validation In PHP</div>
        <form class="form-horizontal" method="post" action="validate.php">         
            <div class="panel-body">                 
                    <div class="form-group">
                        <label class="control-label col-sm-2" for="Name">Name:</label>
                        <div class="col-sm-5">
                          <input type="text" class="form-control" value = "<?php echo $_SESSION['name'];?>" id="name" name="name">
                          <span class="text-danger"><?=$nameError?></span>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="control-label col-sm-2" for="email">Email:</label>
                        <div class="col-sm-5"> 
                          <input type="email" class="form-control" value = "<?php echo $_SESSION['email'];?>" id="email" name="email">
                          <span class="text-danger"><?=$emailError?></span>
                        </div>
                    </div>  
                    <div class="form-group">
                            <label class="control-label col-sm-2" for="mobno">Mobile Number:</label>
                            <div class="col-sm-5">
                              <input type="number" class="form-control" value = "<?php echo $_SESSION['mobno'];?>" id="mobno" name="mobno">
                              <span class="text-danger"><?=$mobnoError?></span>
                            </div>
                    </div>
                <div class="form-group">
                        <label class="control-label col-sm-2" for="contact">Password:</label>
                        <div class="col-sm-5">
                          <input type="password" class="form-control"  id="password" name="password">
                          <span class="text-danger"><?=$passwordError?></span>
                        </div>
                </div>
                 <div class="form-group">
                        <label class="control-label col-sm-2" for="contact">Confirm Password:</label>
                        <div class="col-sm-5">
                           <input type="password" class="form-control"  id="cnfpass" name="cnfpass">                           
                        </div>
                </div>                
                <input type="submit"  name="submit" class="next btn btn-success" value="SUBMIT" id="submit" style='margin-left:30%'/>
            </div>                         
        </form>
      </div>
    </div>
</div> 
</body> 
</html>
Step 2: Create validate.php file and add the below line of codes.

All the server-side validation logic is written in this file. Here we make a regular expression pattern for validating the proper name and mobile number. To validate email we are using PHP inbuilt function FILTER_VALIDATE_EMAIL.

<?php 
session_start();
/* Post data */

$name = $_POST['name'];
$email = $_POST['email'];
$mobno = $_POST['mobno'];
$password = $_POST['password'];
$cnfpass = $_POST['cnfpass'];

/* regular expression pattern */

$namePtr = "/^[a-zA-Z ]+$/";
$mobnoPtr	= '/^[0-9]{10}+$/';
$errSts	= false;

if (!preg_match($namePtr,$name)) {
    $_SESSION['nameError'] = "Name must contain only alphabets and space";	
    $_SESSION['name']	= $name;
    $errSts	= true;
}else{
    $_SESSION['nameError'] = '';
    $errSts	= false;
    $_SESSION['name']	= $name;
}

if(!filter_var($email,FILTER_VALIDATE_EMAIL)) {
    $_SESSION['emailError']= "Please enter valid email id";
    $_SESSION['email']	= $email;
    $errSts	= true;
}else{
    $_SESSION['emailError'] = '';
    $_SESSION['email']	= $email;	
    $errSts	= false;
}

if(!preg_match($mobnoPtr, $mobno)){
    $_SESSION['mobnoError'] =  "Please enter valid mobile number";
    $_SESSION['mobno']	= $mobno;
    $errSts	= true;
}else{
    $_SESSION['mobnoError']	=	'';
    $_SESSION['mobno']	= $mobno;
    $errSts	= false;
}

if($password != $cnfpass || $password=='' || $cnfpass==''){
    $_SESSION['passwordError'] =  "Password and confirm password doesn't match";	 
    $errSts	= true;
}else{
    $errSts	= false;
}

if($errSts){
    header('Location:index.php');
}else{
    echo "<pre>";
    print_r($_POST);
}
?>

 

Above are the steps to implement Server Side Form Validation in PHP.

Get Geolocation from IP Address Using PHP            Auto Save Example In PHP &#038; MYSQL.

Related Articles

Leave a Reply

Check Also
Close
Back to top button