PHP PDO TUTORIALS

Server-Side Validation In PHP

Written by admin
<!DOCTYPE html>
<html lang="en">

<head>
  <title>Server Side validation In PHP</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"> </head>

<body>
  <?php
$nameErr = $emailErr = $mobileErr = $genderErr = $countryErr = $addressErr = NULL;
error_reporting(0);
if (isset($_POST['submit']))
{
    $name = $_POST['name'];
    $email = $_POST['email'];
    $mobile = $_POST['mobile'];
    $gender = $_POST['gender'];
    $country = $_POST['country'];
    $address = $_POST['address'];
    if (empty($name))
    {
        $nameErr = "Name is required";
    }
    elseif ($name)
    {
        if (!preg_match("/^[a-zA-Z ]*$/", $name))
        {
            $nameErr = "Only letters and white space allowed";
        }
    }
    if (empty($email))
    {
        $emailErr = "Email id required";
    }
    elseif ($email)
    {
        $email = test_input($_POST["email"]);
        if (!filter_var($email, FILTER_VALIDATE_EMAIL))
        {
            $emailErr = "Invalid email format";

        }
    }

    if (empty($mobile))
    {
        $mobileErr = "Mobile id required";
    }
    elseif ($mobile)
    {
        if (!preg_match('/^[0-9]{10}+$/', $mobile))
        {
            $mobileErr = "Invalid Mobile Number";

        }
    }
    if (empty($_POST["gender"]))
    {

        $genderErr = "gender required";
    }

    if ($country == 'NA')
    {
        $countryErr = "Country required";
    }

    if (empty($address))
    {
        $addressErr = "Address required";
    }

}

else
{
    //data insert in database
    
}
function test_input($data)
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}
?>
    <div class="container">
      <div class="row">
        <div class="col-md-6">
          <div class="col-lg bg-success">
            <h3><center>Server Side validation in PHP</center></h3>
            <form method="post">
              <div class="form-group col-md-12">
                <label for="name">Name:</label>
                <input type="text" class="form-control" id="name" placeholder="Enter Name" name="name"> <span style="color: red"><?php echo $nameErr; ?></span> </div>
              <div class="form-group col-md-12">
                <label for="email">Email:</label>
                <input type="text" class="form-control" id="email" placeholder="Enter Email" name="email"> <span style="color: red"><?php echo $emailErr; ?></span> </div>
              <div class="form-group col-md-12">
                <label for="mobile">Mobile:</label>
                <input type="text" class="form-control" id="mobile" placeholder="Enter Mobile" name="mobile"> <span style="color: red"><?php echo $mobileErr; ?></span> </div>
              <div class="form-group col-md-12">
                <label for="gender">gender:</label>
                <br>
                <input type="radio" name="gender" value="female">Female
                <input type="radio" name="gender" value="male">Male
                <input type="radio" name="gender" value="other">Other <span style="color: red"><?php echo $genderErr; ?></span> </div>
              <div class="form-group col-md-12">
                <label for="email">Country:</label>
                <select name="country" id="country" class="form-control">
                  <option value="NA">--select--</option>
                  <option value="India">India</option>
                </select> <span style="color: red"><?php echo $countryErr; ?></span> </div>
              <div class="form-group col-md-12">
                <label for="email">Address:</label>
                <textarea name="address" id="address" class="form-control" placeholder="Enter Address"></textarea> <span style="color: red"><?php echo $addressErr; ?></span> </div>
              <div class="form-group col-md-12">
                <input type="submit" name="submit" value="submit"> </div>
              <br> </form>
          </div>
        </div>
      </div>
    </div>
</body>

</html>
Server-Side Validation In PHP With Source Code
Size: 2kb
Version: 2.3

About the author

admin

Hi there! I'm Shiv Gupta. I specialized in building Websites. I write blogs in my free time. I really like to find out and share my knowledge with others.I founded ThePhpConcept in March 2020. I started this blog in order that I can interact with some like-minded people and also help people learning PHP, Mysql, jQuery, Html, and PHP Projects and related technologies.
Support-thephpconcept@gmail.com

2 Comments

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.