Validating form data php
In this tutorial, we will see how to use PHP to validate the data collected from a form.You will see how to validate various fields used in general, like text, list, checkbox, radio button and we will also see how to retain POST data, so that after the user submits the form, even if the data supplied is not valid, data is not lost.Any errors or omissions will be highlighted with a message alongside the relevant field. The purpose of the form is to capture user details (name, address, and email) to obtain feedback (on fruit consumption and favorite fruit) and request a brochure. There are two specific attributes that need to be set in the form tag, .This prevents the form from breaking if the user submits HTML markup and also is a means of protecting against XSS (Cross Site Scripting) attacks, which attackers will use to try to exploit vulnerabilities in web applications.If it has not been submitted, bypass the validation and display a blank form.So far, we have a largely HTML page which defines the form; the only PHP so far is to specify the form’s action value.Iain's specialized areas of teaching is web technologies, mainly programming in a variety of languages, HTML, CSS and database integration.
At the start of the script, before any HTML, you can check whether the form has been submitted using attribute, and I’ve also marked whether or not the field is a required field for validation purposes.
One topic which I have not addressed and is outside the scope of this article is what you would do with the data after validation is successful.
There are a number of possibilities, including saving it in a database or emailing the data to yourself.
Whichever you choose, you need to be sure: Validation is essential, particularly if you are going to save the data in a database – remember the old saying, GIGO (Garbage In, Garbage Out), and you won’t go far wrong.
In this article you have learned how to create a simple HTML form and validate it with PHP.