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.
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!!”.
HTML code: The following code uses the above PHP “form.php” code.
- Incorrect input by user
- Correct input by user
Thank you chetan, 123456789 is correct
Attention reader! Don’t stop learning now. Get hold of all the important HTML concepts with the Web Design for Beginners | HTML course.