In previous articles I’ve looked at creating an accessible form using HTML. At the time I mentioned that there were two things we could not do, at least at that stage:
- Make a form look pretty (solved with CSS for forms)
- Actually do anything with the form information.
We can now solve the latter problem with PHP. But before we send the user’s submitted data anywhere, we must check that they have filled out our form correctly. This is known as “form validation”. (Not to be confused with HTML validation).
There are three steps to validating a form with PHP:
- Check that the entered data is correct. If it is, jump to step 3. Otherwise, go to step 2.
- Display the form again, preserving any information entered this far and highlighting where errors occur. Allow the user to correct any mistakes and submit the form again, returning to step 1.
- When all form data is correct, process it (enter it into a database, send it in an email, etc), and provide some indication to the user that the process is complete.
Enjoy this piece? I invite you to follow me at twitter.com/dudleystorey to learn more.