HTML The Definitive Guide (99 page)

Read HTML The Definitive Guide Online

Authors: Chuck Musciano Bill Kennedy

BOOK: HTML The Definitive Guide
13.99Mb size Format: txt, pdf, ePub

This powerful mechanism has far-reaching implications, particularly for electronic commerce. It finishes an online catalog by giving buyers a way to immediately order products and services. It gives nonprofit organizations a way to sign up new members. It gives market researchers a way to collect user data. It gives you an automated way to interact with your HTML document readers.

Mull over the ways you might want to interact with your readers while we take a look at both the client-and server-side details of creating forms.

10.1 Form Fundamentals

Forms are comprised of one or more text input boxes, clickable buttons, multiple-choice checkboxes, and even pulldown menus and image maps, all placed inside the

tag. You can have more than one form in a document, and within each you may also put regular body content, including text and images. The text is particularly useful for providing instructions to the users on how to fill out the form and for form element labels and prompts. And, within the various form elements, you can use JavaScript event handlers for a variety of effects like testing and verifying form contents and calculating a running sum.

Once a user fills out the various fields in the form, they click a special "Submit" button (or, sometimes, press the Enter or Return key) to submit the form to a server. The browser packages up the
user-supplied values and choices and sends them to a server or to an email address.[1] The server

passes the information along to a supporting program or application that processes the information and creates a reply, usually in HTML. The reply may be simply a thank you or it might prompt the user how to fill out the form correctly or to supply missing fields. The server sends the reply to the browser client, which then presents it to the user. With emailed forms, the information is simply put into someone's mailbox; there is no notification of the form being sent.

[1] Some browsers, Netscape and Internet Explorer in particular, may also encrypt the information, securing it from credit-card thieves, for example. However, the encryption facility must also be supported on the server-side as well: contact the browser manufacturer for details.

The server-side data-processing aspects of forms are not part of the HTML standard; they are defined by the server's software. While a complete discussion of server-side forms programming is beyond the scope of this book, we'd be remiss if we did not include at least a simple example to get you started.

To that end, we've included at the end of this chapter a few skeletal programs that illustrate the common styles of server-side forms programming.

9.5 Applying Styles to

10.2 The Tag

Documents

Chapter 10

Forms

 

10.2 The Tag

Place a form anywhere inside the body of an HTML document with its elements enclosed by the tag and its respective end tag

. You can, and we recommend you often do, include regular body content inside a form to specially label user-input fields and to provide directions.


Function:

Defines a form

Attributes:

ACCEPT-CHARSET ONKEYPRESS

ACTION ONKEYUP

CLASS ONMOUSEDOWN

DIR ONMOUSEMOVE

ENCTYPE ONMOUSEOUT

ID ONMOUSEOVER

LANG ONMOUSEUP

METHOD ONRESET

NAME

ONSUBMIT

ONCLICK STYLE

ONDBLCLICK TARGET

ONKEYDOWN TITLE

End tag:

; never omitted

Contains:

form_content

Used in:

block

Browsers flow the special form elements into the containing paragraphs as if they were small images embedded into the text. There aren't any special layout rules for form elements, so you need to use other HTML elements, like tables and style sheets, to control the placement of elements within the text flow.

You must define at least two special form attributes, which provide the name of the form's processing server and the method by which the parameters are to be sent to the server. A third, optional attribute lets you change how the parameters get encoded for secure transmission over the network.

10.2.1 The action Attribute

The required action attribute for the

tag gives the URL of the application that is to receive and process the form's data.

Most webmasters keep their forms-processing applications in a special directory on their web server, usually named
cgi-bin
, which stands for Common Gateway Interface-binaries.[
2
] Keeping these special forms-processing programs and applications in one directory makes it easier to manage and secure the server.

[2] The Common Gateway Interface (CGI) defines the protocol by which servers interact with programs that process form data.

A typical tag with the action attribute looks like this: ...


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

In general, if you see a URL that references a document in a directory named
cgi-bin
, you can be pretty sure that the document is actually an application that creates the desired page dynamically each time it's invoked.

10.2.2 The enctype Attribute

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

The standard encoding format is the Internet Media Type "application/x-www-form-urlencoded." You can change that encoding with the optional enctype attribute in the

tag. The only optional encoding formats currently supported are "multipart/form-data" and "text/plain."

The multipart/form-data alternative is required for those forms that contain file-selection fields for upload by the user. The text/plain format should be used in conjunction with a mailto URL in the action attribute for sending forms to an email address instead of a server. Unless your forms need file-selection fields or you must use a mailto URL in the action attribute, you probably should ignore this attribute and simply rely upon the browser and your processing server to use the default encoding type.
[File selection controls, 10.5.1.3]

Other books

Southpaw by Rich Wallace
Sky Knights by Alex Powell
American Visa by Juan de Recacoechea
House That Berry Built by Dornford Yates
Quarantined Planet by John Allen Pace
The Sable Moon by Nancy Springer