1.2. Displaying Dynamic HTML Forms

PHP brings new capabilities to HTML forms. Because you can use variables in PHP forms, your forms can now be dynamic: They can be formatted at the time they are generated, rather than predetermined ahead of time as static forms are. The content of the form can change, based on information supplied by the user or information retrieved from the database. Here are the major capabilities that PHP brings to forms:

  • Using variables to display information in input text fields

  • Using variables to build dynamic lists for users to select from

  • Using variables to build dynamic lists of radio buttons

  • Using variables to build dynamic lists of check boxes

1.2.1. Displaying values in text fields

When you display a form on a Web page, you can put information into the text fields rather than just displaying a blank field. For example, if most of your customers live in the United States, you might automatically enter US in the country field when you ask customers for an address. If the customer does indeed live in the United States, you've saved the customer some typing. And if the customer doesn't live in the United States, he or she can just select the appropriate country. Also, the text automatically entered into the field doesn't have any typos — well, unless you included some yourself.

To display a text field that contains information, you use the following format for the input field HTML statements:

<input type="text" name="country" value="US">

This displays ...

Get PHP & MySQL® Web Development All-in-One Desk Reference for Dummies® 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.