This and the next chapters show how to use PHP to validate form data.
PHP Form ValidationThink SECURITY when processing PHP forms!
These pages will show how to process PHP forms with security in mind. Proper validation of form data is important to protect your form from hackers and spammers!
The HTML form we will be working at in these chapters, contains various input fields: required and optional text fields, radio buttons, and a submit button:
The validation rules for the form above are as follows:
Name | Required. + Must only contain letters and whitespace |
Required. + Must contain a valid email address (with @ and .) | |
Website | Optional. If present, it must contain a valid URL |
Comment | Optional. Multi-line input field (textarea) |
Gender | Required. Must select one |
First we will look at the plain HTML code for the form:
Text FieldsThe name, email, and website fields are text input elements, and the comment field is a textarea. The HTML code looks like this:
Name:
E-mail:
Website:
Comment:
The gender fields are radio buttons and the HTML code looks like this:
Gender:
Female
Male
Other
The HTML code of the form looks like this: