Chapter 12. Form Handling
One of the main ways that website users interact with PHP and MySQL is through HTML forms. These were introduced very early on in the development of the World Wide Web, in 1993âeven before the advent of ecommerceâand have remained a mainstay ever since, due to their simplicity and ease of use, although formatting them can be a nightmare.
Of course, enhancements have been made over the years to add extra functionality to HTML form handling, so this chapter will bring you up to speed on the state of the art and show you the best ways to implement forms for good usability and security. Plus, as you will see a little later on, the HTML5 specification has further improved the use of forms.
Building Forms
Handling forms is a multipart process. First is the creation of a form into which a user can enter the required details. This data is then sent to the web server, where it is interpreted, often with some error checking. If the PHP code identifies one or more fields that require reentering, the form may be redisplayed with an error message. When the code is satisfied with the accuracy of the input, it takes some action that may often involve a database, such as entering details about a purchase.
To build a form, you must have at least the following elements:
-
An opening
<form>
and closing</form>
tag -
A submission type specifying either a GET or POST method
-
One or more
input
fields -
The destination URL to which the form data is to be submitted
Get Learning PHP, MySQL & JavaScript, 6th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.