Form Validation in PHP with Examples [2 Steps]

In this article we are going to cover Form Validation in PHP | simple form validation in php examples | simple form validation in php examples using html.

What is Form Validation in PHP?

HTML form contains various input fields such as text box, checkbox , radio buttons, submit button and checklist, etc. This input fields need to be validated, which ensures that the user has entered information in all required fields and also validate that the information provided by the user is valid and correct.

HTML forms are used to send the user information to the server and returns the result back to the browser. You can collect the information by means of form processing. Then, the information can be validated either the client-side or on the server-side. The final result is sent to the client through the respective web browser. To create a HTML form.

Creating a simple HTML Form :

demo.php
<!DOCTYPE html>
<html>
  
<head>
    <title>Simple Form Processing</title>
</head>
  
<body>
    <form action="form_1.php" method="post">
        FirstName:
        <input type="text" name="firstname" required/>
        <br>
        <br> 
        LastName
        <input type="text" name="lastname" required/>
        <br>
        <br> 
        Address
        <input type="text" name="address" required/>
        <br>
        <br> 
        Email Address:
        <input type="email" name="email" required/>
        <br>
        <br> 
        Password:
        <input type="password" name="password" required/>
        <br>
        <br>
        <input type="submit" value=Submit>
    </form>
</body>
</html>

form_1.php

<html>
<body>

Welcome <?php echo $_POST["firstname"] , $_POST["lastname"]; ?><br>
    
Your email address is: <?php echo $_POST["email"]; ?><br>
Your address is: <?php echo $_POST["address"]; ?>

</body>
</html>

Output:

Form Validation in PHP with Examples [2 Steps] 1
Form Validation in PHP with Examples [2 Steps] 2

Form Validation in PHP Example:

Example of form Validation in PHP:

<!DOCTYPE HTML>  
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>  

<?php
// define variables and set to empty values
$nameErr = $lastnameErr = $emailErr = $genderErr = $websiteErr = "";
$name = $lastname = $email = $gender = $address = $website = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
  if (empty($_POST["name"])) {
    $nameErr = "Name is required";
  } else {
    $name = test_input($_POST["name"]);
    // check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z-' ]*$/",$name)) {
      $nameErr = "Only letters and white space allowed";
    }
  }
    
    
  if (empty($_POST["lastname"])) {
    $lastnameErr = "Name is required";
  } else {
    $lastname = test_input($_POST["lastname"]);
    // check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z-' ]*$/",$name)) {
      $lastnameErr = "Only letters and white space allowed";
    }
  }
    
    
  
  if (empty($_POST["email"])) {
    $emailErr = "Email is required";
  } else {
    $email = test_input($_POST["email"]);
    // check if e-mail address is well-formed
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
      $emailErr = "Invalid email format";
    }
  }
    
  if (empty($_POST["website"])) {
    $website = "";
  } else {
    $website = test_input($_POST["website"]);
    // check if URL address syntax is valid (this regular expression also allows dashes in the URL)
    if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) {
      $websiteErr = "Invalid URL";
    }
  }

  if (empty($_POST["address"])) {
    $address = "";
  } else {
    $address = test_input($_POST["address"]);
  }

  if (empty($_POST["gender"])) {
    $genderErr = "Gender is required";
  } else {
    $gender = test_input($_POST["gender"]);
  }
}

function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}
?>

<h2>PHP Form Validation Example</h2>
<p><span class="error">* required field</span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">  
  Name: <input type="text" name="name" value="<?php echo $name;?>">
  <span class="error">* <?php echo $nameErr;?></span>
  <br><br>
Last Name: <input type="text" name="lastname" value="<?php echo $lastname;?>">
  <span class="error">* <?php echo $lastnameErr;?></span>
  <br><br>
  E-mail: <input type="text" name="email" value="<?php echo $email;?>">
  <span class="error">* <?php echo $emailErr;?></span>
  <br><br>
  Website: <input type="text" name="website" value="<?php echo $website;?>">
  <span class="error"><?php echo $websiteErr;?></span>
  <br><br>
  Address: <input name="address" value= "<?php echo $address;?>">
  <br><br>
  Gender:
  <input type="radio" name="gender" <?php if (isset($gender) && $gender=="female") echo "checked";?> value="female">Female
  <input type="radio" name="gender" <?php if (isset($gender) && $gender=="male") echo "checked";?> value="male">Male
  <input type="radio" name="gender" <?php if (isset($gender) && $gender=="other") echo "checked";?> value="other">Other  
  <span class="error">* <?php echo $genderErr;?></span>
  <br><br>
  <input type="submit" name="submit" value="Submit">  
</form>

<?php
echo "<h2>Your Input:</h2>";
echo $name;
echo "<br>";
echo $lastname;
echo "<br>";
echo $email;
echo "<br>";
echo $website;
echo "<br>";
echo $address;
echo "<br>";
echo $gender;
?>

</body>
</html>

Output:

Form Validation in PHP with Examples [2 Steps] 3
Form Validation in PHP with Examples [2 Steps] 4

We have covered Form Validation in PHP.

Conclusion:

We have covered Form Validation in PHP | simple form validation in php examples, simple form validation in php examples using html.

Related Articles:

Reference:

official html validation page

Shweta Mamidwar

I am Shweta Mamidwar working as a Intern in Product Company. Likes to share knowledge.

Leave a Comment

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

Share via
Copy link
Powered by Social Snap