Search

HTML Form Tutorial, Part II

6 min read
1 views

The textarea Element: Capturing Multi‑Line Text

When a form requires more than a single line of input, the textarea element is the go‑to solution. Unlike the input tag, which is limited to a single line, textarea opens up a free‑form canvas that users can scroll, edit, and fill out at their own pace. This flexibility makes it ideal for comments, addresses, descriptions, and any other block of text that benefits from line breaks.

Two optional attributes, cols and rows, allow developers to control the visible dimensions of the box. cols specifies the approximate number of characters that fit per line, while rows sets how many lines are visible without scrolling. If you omit these, browsers will fall back to a default size that varies between platforms, but specifying them can help ensure a consistent appearance across devices.

The wrap attribute governs how text that exceeds the width of the textarea is handled. This attribute accepts three values: off, hard, and soft. Each serves a distinct purpose and is best chosen based on the data the field will store.

Setting wrap="off" tells the browser to leave the text exactly as typed. No automatic line breaks appear, and the carriage‑return characters remain in the submitted data. This is useful when you want to preserve the user’s manual formatting, such as when collecting postal addresses or code snippets where spacing is meaningful.

When wrap="hard" is used, the browser inserts line breaks at the edge of the box. Those breaks become part of the submitted value, meaning the server receives the text exactly as it appears in the textarea. This option is handy when you need to enforce a maximum line length for downstream processing, like preparing the text for an email body that should not exceed a specific width.

The third option, wrap="soft", applies the visual line wrapping but strips any inserted line breaks from the data before it leaves the form. Users see wrapped text, but the server receives a single continuous string. This is often the best choice for free‑form paragraphs where you don’t want to impose formatting on the data but still want a pleasant editing experience.

Choosing the right wrap setting depends on the content type and how you intend to use the information. For structured data such as mailing addresses, off preserves the user’s line breaks. For text that will be programmatically parsed or displayed with fixed line widths, hard ensures consistency. When line breaks are unnecessary and could clutter the data, soft provides a clean solution.

Any text placed between the opening and closing textarea tags becomes the default content shown when the form loads. If you want an empty field, leave the tags adjacent with no characters between them. This can be useful for required fields that you expect the user to fill out from scratch.

Here’s a concise example that demonstrates the core attributes of a textarea and a submit button that posts the data to a server‑side script. The script’s URL is just a placeholder and should be replaced with the actual processing endpoint.

Prompt
<form method="POST" action="/cgi-bin/handler.cgi"></p> <p> <textarea name="message" cols="30" rows="8"></textarea></p> <p> <input type="submit" value="Send"></p> <p></form>

In practice, you’ll combine textarea with other form elements to build a comprehensive data‑collection experience. Keep the wrap attribute in mind when deciding how the text should be handled after submission, and adjust cols and rows to match the anticipated input volume.

The select Element: Drop‑Down and Scrolling Lists

The select element offers a versatile way to present a set of predefined options. Whether you need a compact drop‑down menu or a list box that shows several choices at once, the same underlying tag adapts with a few attributes. The size attribute dictates the visible height of the box; a value of one turns the control into a drop‑down, while larger values reveal a scrolling list.

Adding the multiple attribute - written simply as multiple because its presence is enough to enable the feature - allows users to pick more than one item from the list. This attribute only makes sense for lists that already display multiple rows; when size is omitted and the control collapses into a drop‑down, multiple is ignored.

Inside the select tags you place a series of option elements. Each option represents one selectable item. While the displayed text is the visible label, the optional value attribute provides a machine‑readable value that the server receives. If value is omitted, the element’s text is used as the submitted value. For maximum compatibility with CGI scripts and server frameworks, it’s a good habit to provide explicit value attributes.

The selected attribute marks an option as the default selection when the form loads. Only one option should be marked as selected in a single‑select list to avoid ambiguity. In a multiple‑select list, several options can carry the selected attribute, pre‑selecting them for the user.

When designing a drop‑down, consider the user’s experience. A short list can comfortably fit into a compact menu, while a longer list may be better presented as a scrolling box. The size attribute controls how many rows are visible; a value of four or five strikes a balance between visibility and space conservation. If the list is so extensive that you’d prefer a scroll bar, simply set size to a higher number, or omit it to default to a drop‑down and rely on the browser’s internal scrolling.

Here are two real‑world examples that illustrate the same data set rendered as a drop‑down and as a multiple‑select list. In both cases, the form posts to a CGI script, which will receive the selected values under the name myselect

<select name="myselect">

<option value="1">First</option>

<option value="2" selected>Second</option>

<option value="3">Third</option>

<option value="4">Fourth</option>

<option value="5">Fifth</option>

</select>

<input type="submit" value="Submit">

</form>

Prompt
<form method="POST" action="/cgi-bin/handler.cgi"></p> <p> <select name="myselect" size="4" multiple></p> <p> <option value="4" selected>Fourth</option></p> <p> </select></p> <p></form>

Notice that in the second example, the multiple attribute is combined with size="4", enabling the user to scroll through four visible rows and select both “Second” and “Fourth.” This small addition expands the form’s interactivity with minimal markup.

When constructing a select element, remember that the user’s chosen values will be transmitted to the server as a comma‑separated list for multiple‑select controls. The server-side script must therefore parse that list if it expects individual values. Proper use of value attributes and clear option labels improves both accessibility and data integrity.

In summary, the select element is a lightweight but powerful tool for limiting user input to a known set of options. By tweaking size and adding multiple, you can adapt the same control to a variety of scenarios without rewriting significant portions of your markup.

The option Element: Defining List Items

Each entry in a drop‑down or list box is defined by an option element. Though seemingly trivial, mastering the option tag’s attributes unlocks a range of usability enhancements that keep forms clear and robust.

The most basic form of an option looks like this:

Prompt
<option value="42">The Answer</option>

Here, the value attribute supplies a machine‑friendly identifier that the server receives upon submission. The text “The Answer” is what the user sees. If you omit the value, the browser automatically falls back to using the text itself as the submitted value. While this may suffice for simple scripts, explicit value attributes eliminate ambiguity, especially when the visible label is more descriptive than the desired data representation.

To pre‑select an option when the form loads, you add the selected attribute:

Prompt
<option value="blue" selected>Blue</option>

Only one selected option should be used in a single‑select list to avoid confusing the user. In multiple‑select lists, you can mark several options as selected to give the user a starting point that reflects typical choices.

Beyond selected, there are two additional attributes that influence the option’s behavior and appearance: disabled and label. Setting disabled prevents the option from being chosen and visually greys it out. This is handy when you want to group choices but keep certain items temporarily unavailable.

Prompt
<option value="premium" disabled>Premium Plan</option>

The label attribute allows you to specify an alternative display text that appears in the drop‑down list while keeping the internal value separate. If the visible label is lengthy, the label attribute can truncate it for a cleaner interface.

Accessibility is a critical consideration when working with option elements. Screen readers interpret the option tag’s text as the label for the control. Therefore, keep the displayed text concise and free of extraneous punctuation. Avoid duplicating the same text across multiple options unless it truly represents distinct choices.

When building forms that will be processed by server-side scripts or JavaScript, test the output of your option values. Ensure that each value is unique when uniqueness is required, such as selecting a product ID or a code. Duplicate values can lead to ambiguous server responses and hard‑to‑debug errors.

Consider a real‑world scenario where you ask users to pick their favorite fruit. You might set up the list like this:

Prompt
<select name="fruit"></p> <p> <option value="ap" selected>Apple</option></p> <p> <option value="or">Orange</option></p> <p> <option value="bk">Banana</option></p> <p> <option value="gr">Grapes</option></p> <p></select>

Here, the values are short two‑letter codes that your server can interpret efficiently. The labels provide readable options to the user. The default selection is “Apple,” but users can change it as they wish.

Finally, remember that option tags belong inside a select container. Nesting them correctly ensures proper rendering across all browsers. A malformed structure - such as placing an option outside of a select - will likely result in broken UI or no selectable values.

By applying these principles - explicit values, clear labels, appropriate selection and disabling - you’ll craft forms that are both user‑friendly and easy for your backend to process.

Putting It All Together: A Full‑Featured Form Example

Now that the individual elements are clear, let’s assemble a comprehensive form that leverages hidden fields, text inputs, checkboxes, radio buttons, file uploads, and the various selection controls we’ve discussed. This example demonstrates a realistic scenario: a user visiting a website, signing in, uploading a file, and providing a comment.

Below is the complete HTML markup for the form. Notice that it includes enctype="multipart/form-data" to allow file uploads and method="POST" to transmit data securely. The action points to a placeholder script; replace this with the actual endpoint that will process the data.

Prompt
<form method="POST" action="/cgi-bin/formupload/handler.cgi" enctype="multipart/form-data"></p> <p> <input type="hidden" name="token" value="xyz123"></p> <p> <p></p> <p> What is your home page URL?<br></p> <p> <input type="text" name="url" size="30" maxlength="200"></p> <p> </p></p> <p> <p></p> <p> What is your password?<br></p> <p> <input type="password" name="pass" size="20" maxlength="50"></p> <p> </p></p> <p> <p></p> <p> Select one or more colors that you like:<br></p> <p> <input type="checkbox" name="color_blue" value="yes">Blue<br></p> <p> <input type="checkbox" name="color_red" value="yes" checked>Red<br></p> <p> <input type="checkbox" name="color_pink" value="yes" checked>Pink<br></p> <p> <input type="checkbox" name="color_yellow" value="yes">Yellow</p></p> <p> <p></p> <p> What is your favorite color?<br></p> <p> <input type="radio" name="favcolor" value="blue">Blue<br></p> <p> <input type="radio" name="favcolor" value="red" checked>Red<br></p> <p> <input type="radio" name="favcolor" value="pink">Pink</p></p> <p> <p></p> <p> Upload any type of file:<br></p> <p> <input type="file" name="userfile"></p> <p> </p></p> <p> <p></p> <p> <input type="button" value="Give me a message" onclick="alert('Thank you!')"></p> <p> </p></p> <p> <p></p> <p> Tell me why you are here!<br></p> <p> <textarea name="message" cols="30" rows="6"></textarea></p> <p> </p></p> <p> <p></p> <p> What is your most <b>un</b>favorite color:<br></p> <p> <select name="unfavcolor"></p> <p> <option value="green">Green</option></p> <p> <option value="red">Red</option></p> <p> <option value="blue" selected>Blue</option></p> <p> <option value="yellow">Yellow</option></p> <p> <option value="pink">Pink</option></p> <p> </select></p> <p> </p></p> <p> <p></p> <p> Select one or more colors you do not like:<br></p> <p> <select name="notliked" size="4" multiple></p> <p> <option value="red" selected>Red</option></p> <p> </select></p> <p> </p></p> <p> <p></p> <p> <input type="image" src="myimage.gif" alt="Send" border="0"></p> <p> </p></p> <p> <p></p> <p> <input type="reset" value="Erase Everything!"></p> <p> </p></p> <p> <p></p> <p> <input type="submit" name="visit" value="This is my first visit"></p> <p> <input type="submit" name="return" value="I've been here before"></p> <p> </p></p> <p></form>

Once the form is placed on a live web page, users can interact with all the controls. Upon clicking one of the submit buttons, the browser packages the data - including the selected file - into a multipart form and posts it to the specified script. The server script can then read each value, store the file in a designated directory, and display a confirmation page that echoes the submitted data.

To help developers get started, a zip archive containing the HTML page, an example image, the processing script, and installation notes is available. You can download it from http://willmaster.com/master/cgi-bin/arts/pl.pl?formtut. Unzip the package, upload the files to a directory on your server, and point your browser to the form.html file to see the form in action.

Suggest a Correction

Found an error or have a suggestion? Let us know and we'll review it.

Share this article

Comments (0)

Please sign in to leave a comment.

No comments yet. Be the first to comment!

Related Articles