Thursday, October 16, 2025
HomeLanguagesHow to display an error for invalid input with php/html ?

How to display an error for invalid input with php/html ?

 PHP can be easily embedded in HTML files and HTML codes can also be written in a PHP file. The thing that differentiates PHP from a client-side language like HTML is, PHP codes are executed on the server whereas HTML codes are directly rendered on the browser. To display error for invalid input with HTML and PHP. 

Approach:

Display error on invalid error when

  • input textbox is left empty.
  • input is wrong.

PHP code: The following is the code for “form.php” which is used in the HTML code in the later part.

To show invalid input in PHP, set the name of the input textbox which is in HTML. All the fields are first checked for empty fields and then it is validated for correctness. If all fields are correct then it shows the success message. If the input given by the user is wrong then it will show a message for ” Invalid input!!”.

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 == 'test@email.com') {
            $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!!!";
}
?>


HTML code: 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="yourname@gamil.com">
  
        <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
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS