Lesson 11

Creating Forms

Forms are ubiquitous on websites. They include not only obvious “fill in the blank” forms but also are the method used to interact with the user. Search boxes and drop-down filters, for instance, are contained within forms.

In this lesson you review the HTML needed to code a form. Then you learn how to use PHP to process that form and gather the responses. Finally, you learn how to send users to the right page after they have submitted the form.

Setting Up Forms

Forms are set up primarily using HMTL code. This is a basic form that asks for your name (see Figure 11-1):

<html>
<head>
  <title>Contact</title>
</head>
<body>
<form action="index.php" method="get">
  <fieldset>
  <legend>Contact</legend>
    <label for="fullname">Name</label>
    <input id="fullname" name="fullname" type="text" />
    <input name="contactForm" type="submit" value="Submit" />
  </fieldset>
</form>
</body>
</html>

The <form> tag sets the action that occurs when the Submit button is clicked. This is the URI of the program that processes the form. It can either be a file specifically for processing forms or it can be an existing file that checks for whether form data was sent and automatically processes it. You learn how to do that later in this lesson.

The <form> tag also assigns the method that is used to send the form data. You learned about the two methods, GET and POST, in Lesson ...

Get PHP and MySQL® 24-Hour Trainer now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.