=========================================================================== Today on The World Vol. 4 #163 Wednesday, June 24, 1998 =========================================================================== Continuing the HTML lessons... Remember that you can find the other chapters on http://world.std.com/help/web/tutorial for now. (kibo) --------------------------------------------------------------------------- HTML TUTORIAL -- CHAPTER 12 -- FORMS 12.1 Forms & CGIs For a Web page to get some input from its readers and then act upon this input requires two things: a
...
section of HTML, and a CGI (Common Gateway Interface) program running on the Web server. CGIs are programs that get data from the reader's Web browser (usually when they submit a form) and do something with this data, sometimes generating a "custom" page based on the input data, other times simply logging the data or mailing it. You can do arbitrary things with CGIs (providing you can write your own CGI programs) but for purposes of this chapter I'm only going to talk about one on The World which turns an HTML form into an E-mail message. (This lesson will not necessarily apply to building Web pages hosted on other Internet Service Providers because they may provide a different CGI for turning forms into E-mail.) Let's get started with your first form. Add this to the body of one of your practice Web pages:

What is your favorite flavor of Pez?

What is your favorite color of Pez?

View that in your browser and see what happens. We've made a non-working form (there's no way to submit the data, and it hasn't been told where to send it anyway) but you should have two blanks that you can use to type text. Notice the structure of our minimalist form:
...
contains the entire form, including some "regular" text that gives the reader instructions. The data is entered into fields created by tags (one or more of them). There is no , meaning is one of those few HTML tags that doesn't need a closing tag (,
,
, etc.) To make this form actually work we need to add two things. First, we need to add a "Submit" button. (We can also add a "Reset" button that lets users clear the form, but these are usually unnecessary -- "Submit" is required, "Reset" aka "Clear" is optional.) Add this at the end of the form, right before : Now when you view the page you should have two buttons at the bottom, labelled with "Submit" and "Reset". (They won't do anything yet.) Let's change their text by embellishing those two tags: Bravo! Big buttons! You've probably noticed that these buttons are tags, just like the text fields. So TYPE="TEXT" makes a text-entry slot, and TYPE="SUBMIT" and TYPE="RESET" make buttons. You'll see some other types of later. For now, let's finish making this form functional.
requires parameters that tell the Web browser how to deliver the data, and to where. Change to: What's that? Well, METHOD="POST" is one of the two ways of getting data to a CGI program. (The other, METHOD="GET", is usually used by search engines such as Altavista because it packs all the submitted data onto the URL so that people can bookmark it. METHOD="POST" sends the data without displaying a funny URL.) ACTION="..." specifies the URL of a CGI program that will be given this data, in this case a Perl script (.pl file) which will read the data from the form and E-mail it to you. But it still won't work yet -- mailto.pl needs to be told where to mail the data! Add this right after the line, and be sure to put your own E-mail address in place of the sample one: All those fields are required for mailto.pl to work (if any of them is missing, it can't make a valid E-mail message, and you'll see some error such as "The request cannot be fulfilled.") Note also that mailto.pl is case-sensitive about the names "to", "body", etc. TYPE="HIDDEN" means these input fields are not visible by the reader, and we fill them in ourselves with VALUE="...". In general, every field can have a NAME="..." option to specify the name of the field and VALUE="..." for the pre-set value of the field. The "nexturl" field is a mailto.pl feature where you specify what page should be displayed after the form has been submitted. This is to make sure that people know that the form has been sent successfully so that they don't keep clicking the "Submit" button. For this example I've set it to go to The World's home page -- you could plug in anything else you like. Try filling out the form we've created, then click the "Submit" button, and check your E-mail. (It may take a minute or two for the mail to get to you, but usually it happens within seconds.) You should receive a message similar to this: From: yourname@world.std.com To: yourname@world.std.com Date: Wed, 24 Jun 1998 17:33:54 -0400 Subject: Testing a Web form. X-Mail-Gateway: Doug's WWW Mail Gateway 2.2 X-Real-Host-From: ppp-666.std.com Here are my Pez favorites. FLAVOR -> raspberry COLOR -> orange The message lists all the pairs of names and values from your fields (with the hidden "from", "to", etc. being used to make the headers of the message, and the "body" field creates additional text above the list of values.) What you do with these messages when you get them is up to you. You could read them and act on them yourself, or you could forward them to a program you wrote to automatically process them. (I like to do surveys by saving a hundred of these to the same file, then sorting the lines alphabetically, then counting the number of repeated lines.) But what if we want to have the form actually say what the E-mail address of the sender is, instead of the "from" value we typed in? Sadly, that's not something we can do automatically -- mainly because the Web server usually can't figure out people's addresses on its own (which is good in that it keeps people from "harvesting" your address when you visit their Web page, i.e. if they could get your address automatically they would send you junk mail.) We can at least allow people to type in an address if they want you to know who they are. Please take out the line and add this paragraph:

What is your favorite flavor of Pez?

This tag has a name for the field ("from"), it's a text field, and it has a pre-defined value (but it can be changed because it's TYPE="TEXT" and not TYPE="HIDDEN".) We specified the default value here because some people are too lazy to fill out forms completely, and mailto.pl will fail if they were to submit a form with a blank "from" field. Now we have a complete form, albeit one which only uses plain text inputs. 12.2 Fancy Form Fields For added fun, let's change the formatting of the form a little. We can make the blanks (for people to type into) wider or narrower. Change the two Pez tags to read: and What happens when you use the form now? You should see two blanks of different lengths. What happens if you type more than five letters into the short one? SIZE, for , specifies how wide the field looks on the screen, now how many characters it can hold. To limit people to five-color flavors, we would use: Now it's physically impossible to enter long words for the flavor. What if we wanted people to be able to enter lots of text, such as a whole paragraph or page? Add the following paragraph before the "Submit" button:

Please type your suggestions for new flavors.

Check that out with your browser. There should now be a big rectangle 50 characters wide, 8 lines tall. Note that it scrolls when you type more than 8 lines into it (at least in normal browsers); there's no way to limit how much data can be entered in a .) Note that