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 userInvalid input!!!Correct input by user Thank you chetan, 123456789 is correct Comment More infoAdvertise with us Next Article How to Display an Error for Invalid Input with PHP/HTML? chetanjha888 Follow Improve Article Tags : PHP Technical Scripter 2020 HTML-Misc PHP-Misc PHP-Questions +1 More Similar Reads How to Log Errors and Warnings into a File in PHP? In PHP, errors and warnings can be logged into a file by using a PHP script and changing the configuration of the php.ini file. Two such approaches are mentioned below: Approach 1: Using error_log() FunctionThe error_log() function can be used to send error messages to a given file. The first argume 3 min read How do I get PHP errors to display? There are four ways to display errors in PHP which are listed below:Â error_reporting: It does not display the E-STRICT, E-NOTICE and E_DEPRECATED level errors and display all the other level errors.display_errors: Its default value is "off". Set it to "on".log_errors: It's default value is "on" whi 2 min read How to perform form validation for a required field in HTML ? Performing form validation for a required field in HTML ensures that a user must fill in specific fields before submitting a form. This is typically done using the required attribute, which prevents form submission if the field is left blank, ensuring proper data input.Table of ContentRequired attri 4 min read How To Validate Input Field In The HTML Form? Input fields are essential components of HTML forms, enabling users to provide data for processing. Ensuring proper validation of these input fields enhances user experience and prevents errors or malicious inputs. What We Are Going to Create?We will create simple Validating Input Fields in HTML For 3 min read How to display error without alert box using JavaScript ? JavaScript errors are displayed using alert boxes, which can interrupt user experience and feel outdated. But Now, there are many alternatives to alert boxes for displaying error messages in a more user-friendly way. This article covers different methods to display errors dynamically without using a 3 min read Like