| |
HTML Forms |
||
| Contents
|
|
What All Forms Need |
|||||
There are many options available for forms, but there are also some things all forms need. Let's look at them first; then we can check out the options. |
|||||
| Form Tags | The Form tags
define the beginning and end of each form. In theory, a page can contain more than one form, but in practice only one is put on each page. That's because only one form can be submitted at a time, and once a form is submitted the user needs to see a new page. The opening tag needs to contain information about:
MethodThis tells the browser how to send the info. There are two possible methods: Get and Post.
ActionThis tells the browser what the server is supposed to do when it receives the info. There are two general techniques:
ScriptsIn this module, we'll be using the email option, because we don't expect you to have programming knowledge. Also, when you receive email with CGI you'll get a good idea of what CGI actually looks like when it's sent from a browser to a server. However, there are a number of places where you can get scripts without having to write them.
|
||||
| Submit Button |
The submit button is the main way to have your users send the information to you. (Also available is the image map, but this is not commonly used, and will not be discussed here.) In most cases, the submit button is just used to send the info from browser to server. Here's code for the simplest form:
This results in a button that says, "Submit Query". If you don't have any other input, what does this send to the server? Nothing! If the form's Action is "mailto:..." the user will be given a blank email document to fill in. You can also give the Submit button a Value and a Name. The Name does nothing, but the Value changes the text that appears on the button. You can even create multiple Submit buttons; if you give each a name, they're supposed to send the Value to the server. I've found (in "mailto:" actions at least) that the information is still blank. Here's example code and its output:
|
||||
| Try It! | Using the code above as your model, create a page for the SCHM with three submit buttons: one for volunteering in the morning, one for afternoon, and one for evening. Change the "mailto:" address to your own, so you can see what is submitted. When you get the form to show in your browser, test each button and check your email to see what happens. |
||||
Text Input |
|||||
|
Text is probably the most common form of information input via forms. There are three options: text boxes, text areas, and password boxes: |
|||||
| Text Boxes |
Text boxes are for typing short pieces of information. Here's an example of the simplest code for a text box:
The result is a simple box for the user to type in: This is all you get: no label or clue of any kind what to do with it! Clearly we need more than this. Here's a more complete example:
|
||||
| Text Areas |
Text areas offer more space than text boxes, including scrolling. They're ideal for letting the user give you feedback, or for information that doesn't have to be structured ahead of time - or can't be. Coding textareas is a little different from the inputs we've seen so far. Instead of just one tag, you need an opening and a closing tag. In between, if you like, you can put text you'd like to have appear in the area when the page is first loaded:
Usually the space between the tags if left blank, and a prompt is put in above the textarea. Textareas can hold *lots* of text - over 32,000 characters. Scrollbars
will come into action if someone types enough to need them. However, they
don't automatically word-wrap unless you put the parameter If you don't give a size, textareas use a default size:
You can control height and width with the
As an example, here's a simple form to get feedback from SCHM visitors:
|
||||
| Password Boxes |
Password boxes are just like text boxes, except that when you type in them they show stars instead of letters. That way, people can't see your password if they're looking over your shoulder. However, the contents aren't encrypted, so they could be detected by hackers. |
||||
| Hidden Fields |
Often, we need to send information to the server that the user isn't typing in. This makes it possible to use more general programs on the server. For example, there is a well-know CGI program called formmail that has been used by thousands of Webmasters to interpret user data. The program is given instructions using hidden fields - information put in by the person writing the form, rather than by the user. |
||||
| Try it! |
Add these fields to the SCHM form you created above:
Which type of field should each of these be? |
||||
Buttons, Boxes, and Menus |
|||||
When you have choices to offer to people, you can offer them as either radio buttons, check-boxes, or menus. |
|||||
| Radio-Buttons |
Named after the type of buttons often found on car radios, these let you pick one and only one "station to listen to". Coding radio buttons is similar to coding input fields, except
that type="radio", and you need to include a
There aren't any opening or closing tags for radio buttons that are part
of a set. All input fields with the same Example: Sherry Moore wants to know what school district the volunteers reside in, so she can arrange learning activities with school teachers. Stony Creek is in the Lincoln Consolidated school district, but other nearby districts may produce volunteers. Here's a form to collect that information:
|
||||
| Check-Boxes |
Checkboxes let you click to check as many options as you like. Coding checkboxes is similar to coding radio buttons: there are
no beginning and ending tags, and members of the same set are identified
by their common
Example: Sherry needs to know what type of work volunteers can do. Checkboxes are an ideal way of capturing that information. Here's code to do that:
|
||||
| Menus |
Menus are used to get the same kinds of information as radio buttons - items where there is one and only one choice for the user. You can use menus rather than radio buttons when there is a long list of options, and you don't want to take up a lot of space on your page listing them all. (Radio buttons are useful when you want your visitors to be able to see all the options at once.) Code for menus involves two kinds of tags: opening and closing
Ordinarily, you can only make one choice on a menu, but with the parameter
Example: If Sherry wants volunteers to indicate the one day of the week they most prefer to work, we can create her a menu like this:
|
||||
| Try it! |
Add these fields, with appropriate labels, to your SCHM volunteer form:
|
||||
Bells and Whistles |
|||||||
There are also a number of options which may or may not be available in all browsers, but which you may want to consider. These include hidden fields, a Reset button, using images to submit data, organizing with Fieldset and Legend tags, labels, tab order, disabled fields, keeping users from changing fields, and keyboard shortcuts. We'll only discuss a couple here. |
|||||||
| Reset Button |
Occasionally, for whatever reason, a visitor wants to start completely over again. Browsers can empty all the fields and let them do that if you include a reset button. These are usually put next to the submit button, but could be placed anywhere in the form. Coding a reset button is simple:
This will cause the browser to show a button simply labelled "Reset". If you like, you can add a VALUE parameter to give a different label to the button.
|
||||||
| Tab Order |
Many people move from one field to another using the mouse, but it's also possible to use the Tab key. The Tab key can speed things up, especially on long, tiresome forms. In simple forms, the Tab key will take users to the next field in an intuitive way, but when tables with multiple columns are used, the Tab key may lead people in strange and confusing ways. Coding tab order is done by adding a
One hitch with tab order in some browsers is that the first time someone
presses the Tab key, input goes to the browser's URL window rather than
to the first field in the tab order. You can think of the URL window in
these browsers always having a tabindex of zero. In addition to form fields
and buttons, tab order can also be assigned to links by adding the Example: using a four-column table can make the form for entering name and contact info much neater, but it can also result in the tab order being strange. The first example shows a table without tab order. (To save space, the code isn't shown.) Try using the Tab key to move from one field to the next - it follows the HTML code order, not the logical sequence:
The second example shows the same code, but with tabindex in each input
field. (Notice the
|
||||||
|
|
|---|---|
Audience: |
This is for people who are familiar with HTML, up to and including links and images, who want to learn how to build the user side of an interactive Web form. |
Objectives: |
When you successfully complete this lesson, you will be able to...
|
Module W26c: |
This document is part of a modular instruction series in Computer Instruction. For more information, see the overview or the list of modules in this series, W: World Wide Web. This document has been used in the following classes: INP 150. |
Author: |
Laurence J. Krieg |
Institution: |
|
| History: | Original:
21 November 2002 Last modification: Monday, 31-Aug-2009 11:48:06 EDT |
| Copyright: | Copyright
© 2003, Laurence J. Krieg. Instructors: You may point to this file in your Web-based materials. Students: you may make a copy for your personal use. All other uses: contact the author, Laurence J. Krieg for permission. Email krieg@ieee.org |