Open In App

How to Display an Error for Invalid Input with PHP/HTML?

Last Updated : 23 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

PHP can be easily embedded in HTML files, and HTML code can also be written in a PHP file. The key difference between PHP and a client-side language like HTML is that PHP code is executed on the server, while HTML code is directly rendered in the browser. To display errors for invalid input using HTML and PHP, follow this approach:

  • Display an error message when the input textbox is left empty.
  • Display an error message when the input is invalid.

The following code for “form.php” demonstrates how to handle invalid input in PHP. First, set the name of the input textbox in HTML. All fields are checked for emptiness and then validated for correctness. If all fields are correct, a success message is shown. If the input is invalid, an “Invalid input!!” message is displayed.

PHP
<?php

$nameError = "";
$emailError = "";
$passwordError = "";
$mobileError = "";
$success = "";
 
function validate_input($input) {
    $input = trim($input);
    $input = stripslashes($input);
    $input = htmlspecialchars($input);
    return $input;
}
 
if(isset($_POST['form_submit'])) {
    $name = $_POST['name'];
    $password = $_POST['password'];
    $email = $_POST['user_email'];
    $mobile = $_POST['mobile'];
   
    if (empty($_POST["name"])) {
        $nameError = "Name is required";
    } else {
        $name = validate_input($_POST["name"]);
        
        if($name == 'chetan') {
            $success= "Thank you ". $name.", ";
            echo $success;
        }
    }
   
    if (empty($_POST["email"])) {
        $emailError = "Email is required";
    } else {
        $email = validate_input($_POST["email"]);
        
        if($email == '[email protected]') {
            $success= $email." is correct";
            echo $success;
        }
    }
     
    if (empty($_POST["password"])) {
        $passwordError = "Password is required";
    } else {
        $password = validate_input($_POST["password"]);
    
        if($password == 'test@123') {
            $success= $password." is correct";
            echo $success;
        }
    }
 
    if (empty($_POST["mobile"])) {
        $mobileError = "Mobile is required";
    } else {
        $mobile = validate_input($_POST["mobile"]);
        
        if($mobile == '123456789') {
            $success= $mobile." is correct";
            echo $success;
        }
    }
    if(empty($success))
        echo "Invalid input!!!";
}
?>

The following code uses the above PHP “form.php” code.

HTML
<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
    <meta charset="utf-8">
    <title>Form</title>
</head>

<body>
    <form action="form.php" method="POST">
        <input type="text" name="name" 
            placeholder="Enter name">

        <input type="password" name="password"
            placeholder="Password">

        <input type="email" name="user_email" 
            placeholder="[email protected]">

        <input type="tel" name="mobile" 
            placeholder="Mobile no">

        <button type="submit" name="form_submit">
            Submit
        </button>
    </form>
</body>

</html>

Output:

  • Incorrect input by user
    Invalid input!!!
  • Correct input by user 
    Thank you chetan, 123456789 is correct


Next Article

Similar Reads