Forms, the FORM Tag and Attributes, and Scripts

Because forms are so bound up with scripting languages, some discussion of scripts and scripting languages, and how scripts relate to forms, is necessary. This discussion will entail some jumping around between regular HTML and more esoteric scripting language information; please bear with me.

First Principle: a form is created using the FORM tag. It goes in the BODY of a regular HTML page, just like most other HTML tags.

Tag: FORM
Description: this tag marks the boundaries of an HTML form. This tag MUST close. There may be more than one form on an HTML page. Each FORM tag will have ONE submit button (or simulated submit button) associated with it. Note: If you only see one submit button on a page, you can be sure that there's only one FORM tag present. There will always be one submit button for each FORM tag; conversely, there will NEVER be any FORM tag WITHOUT a submit button (or simulated submit button). We will talk about creating form elements and submit buttons in the next section.

Example:

<form>

<!-- one or more form elements, with their accompanying HTML context -->

<!-- a submit button -->

</form>

Before we can go any further into the HTML code involved with the FORM tag, we need to talk more about CGI scripts.

CGI stands for Common Gateway Interface. All web servers have CGI capabilities. The CGI is the means for web pages to communicate with the server directly, with power to read and rewrite information on the server hard drive, whether in a database or in a more ordinary directory or file.

CGI scripts can be written in any programming language, including C, C++, Java, and Perl. Perl is the most common programming language used for writing CGI scripts as regards web pages and forms; this is why CGI scripts are often referred to as CGI/Perl scripts.

Because CGI scripts have the power to rewrite or erase a web server's hard drive, they are considered to be a potential security hazard or security breach by web server administrators (with good reason!). Unless you have special privileges or direct physical access to an actual web server computer, you will probably not be allowed to write CGI scripts yourself.

Web servers have something called the "cgi-bin", which is simply a directory on the server which contains all of the CGI scripts for that server. These CGI scripts may be accessed if you know the URL for the script and its "cgi-bin" directory. Most ISPs (Internet Server Providers) have a collection of CGI scripts available to their customers, and have published FAQ sheets on their website which give information on how to connect your HTML pages and HTML forms to these pre-made CGI scripts.

Have you ever seen a "counter" on a web page which tells you how many people have visited that page? That counter is created using a commonly-available CGI script. There is also a very common CGI script which takes the contents of an HTML form, converts that information into text, inserts the text into the body of an email message, and sends that email to whomever has been specified. These scripts, and many more, are usually readily available in the cgi-bin of any ISP.

If you register your own domain name in combination with an ISP, your ISP server administrator may be able to give you a separate cgi-bin for your domain. If you arrange for a separate cgi-bin for your domain, you will no longer have access to the main cgi-bin for the ISP (with its accompanying scripts), but you WILL be able to write your own CGI scripts, if that is important to you. Again, if you work for a company with its own web server and you have access privileges to the actual web server computer, you will probably be able to write CGI scripts and drop them directly into the cgi-bin on that computer.

CGI scripts usually come with one of two dot-extensions on the file name, ".cgi" (for a generic CGI script) and ".prl" (for a Perl script specifically).

Now, back to the FORM tag...

The FORM tag has a few attributes which relate to connecting it to a script. You will NOT be using these attributes in the exercises and examples for this module, but you will need to understand what they are, what they do, and what type of scripts they relate to.

Remember: for the exercises in this module, you will NOT be using ANY attributes for the FORM tag; you will only need the attributes under discussion when you connect your HTML form to a CGI/Perl or JavaScript script.

Two attributes for the FORM tag are used when connecting a form to a CGI script: METHOD and ACTION.

Tag: FORM
Attribute: METHOD
Value: get (default) or post
Description: this attribute defines the manner in which the form information is conveyed to the script; we'll talk more about this in a later section.

Example (abbreviated):

<form method="post"></form>

Tag: FORM
Attribute: ACTION
Value: a URL (to the CGI script in the cgi-bin in question)
Description: the ACTION attribute of the FORM tag connects that form to a CGI script; when the submit button for the form is pressed, the web browser seeks out the CGI script at the URL indicated by the value of ACTION and causes the web server to execute that CGI script.

Example (abbreviated):

<form method="post" action="/cgi-bin/myScript.cgi"></form>

Again, both the METHOD and the ACTION attributes are required when connecting a form to a CGI script.

There is a third attribute of FORM which is occasionally used by JavaScript scripts: NAME. JavaScript doesn't REQUIRE any attributes of the FORM tag in order to process a given form, but sometimes you'll want to NAME your form in order to access the form elements directly.

Tag: FORM
Attribute: NAME
Value: any name (adhering to naming guidelines outlined in Modules 1 and 7)
Description: naming a FORM allows a JavaScript to access elements of that form by THEIR names and change their values; the details of this process are outside the scope of this class.

Example (abbreviated):

<form name="fred"></form>

Note: In the examples given above, I have used empty FORM tags in order to show you the attributes in their opening FORM tags; you would NEVER use empty FORM tags in real life.

I have a little more to say about the METHOD attribute, but it will have to wait until after I introduce the INPUT tag in the next section.

Having said all this about FORM attributes, I will now restate that you will not be using ANY of these attributes during this module (with the possible exception of METHOD). Attributes of the FORM tag should generally be excluded from your code (for testing purposes) until AFTER you have finished and tested the HTML portion of the form itself; otherwise, you may have your browser attempting to access non-existent CGI scripts and throwing up error messages. Once you have your form beautifully laid out and tested, only THEN should you add the appropriate attributes to the FORM tag and test that form with the actual script attached.

Main Menu