home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam    

Book HomeWebmaster in a Nutshell, 3rd EditionSearch this book

Chapter 6. Forms

Most CGI programs use HTML forms to gather user input. Forms are comprised of one or more text-input boxes, clickable radio buttons, multiple-choice checkboxes, and even pull-down menus and clickable images, all placed inside the <form> tag. Within a form, you may also put regular body content, including text and images. The JavaScript event handlers can be used in various form elements as well, providing a number of effects such as testing and verifying form contents.

6.1. The <form> Tag

You place a form anywhere inside the body of an HTML document with its elements enclosed by the <form> tag and its respective end tag </form>. All of the form elements within a <form> tag comprise a single form. The browser sends all of the values of these elements—blank, default, or user-modified—when the user submits the form to the server.

The required action attribute for the <form> tag gives the URL of the application that is to receive and process the form's data. A typical <form> tag with the action attribute looks like this:

<form action="http://www.oreilly.com/cgi-bin/update" >

The example URL tells the browser to contact the server named www.oreilly.com and pass along the user's form values to the application named update, located in the cgi-bin directory.

The browser specially encodes the form's data before it passes the data to the server so it doesn't become scrambled or corrupted during the transmission. It's up to the server to decode the parameters or pass them, still encoded, to the application.

The standard encoding format is the media type named application/x-www-form-urlencoded. You can change that encoding with the optional enctype attribute in the <form> tag. If you do elect to use an alternative encoding, the only other supported format is multipart/form-data.

The standard encoding—application/x-www-form-urlencoded—converts any spaces in the form values to a plus sign (+), nonalphanumeric characters into a percent sign (%) followed by two hexadecimal digits that are the ASCII code of the character, and the line breaks in multiline form data into %0D%0A. (See Chapter 12 for more information on URL encoding.)

The multipart/form-data encoding encapsulates the fields in the form as several parts of a single MIME-compatible compound document.

The other required attribute for the <form> tag sets the method by which the browser sends the form's data to the server for processing. There are two ways: the POST method and the GET method. See Chapter 12 for more information on GET and POST.

Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.