Lab 3: Advanced HTML

Thu Oct 4 15:39:44 EDT 2001

By the end of this lab, you will be able to:

In Lab 2, you learned a lot of the basics of HTML. This week you get to experiment with three of the more advanced features: tables, forms, and frames. Hopefully, these will give you the flexibility you need to really develop your web site.

At the same time, once you've mastered this week's topics, it will be easier to deal with future Web technologies. Even though this course will be over, somewhere on the Web, there will always be sites like this one which can tell you what you need to know!

Part 1: Tables
Part 2: Forms
Part 3: Frames
Part 4: Finishing up


Part 1: Tables

What are tables?

Basically, tables are boxes, divided into rows and columns, which let you organize your web page. If the information you want to display is too complicated for a list, or you can't get a group of pictures to align properly, a well-designed table may be the solution.

Here's a table with two rows and three columns
Column 1 Column 2 Column 3

After we cover the basic tags, we're going to show you different options that let you customize your table to fit your needs. At first the number of options may seem overwhelming, but once you've created a table of your own, you will appreciate the freedom they give you.

Basic table construction

Here is one of the most basic tables you can create:

1 2 3
4 5 6
7 8 9

It doesn't look much like a table, since it doesn't have any fancy borders, headings, or shading, but it is a good place to start since it doesn't require you to include any extra attributes or tags.

Here's the HTML code that created that particular table:

<TABLE>
<TR> <TD>1</TD> <TD>2</TD> <TD>3</TD> </TR>
<TR> <TD>4</TD> <TD>5</TD> <TD>6</TD> </TR>
<TR> <TD>7</TD> <TD>8</TD> <TD>9</TD> </TR>
</TABLE>

Notice how each number is surrounded by <TD></TD>. This creates a cell. Every three of these cells are also surrounded by <TR></TR>. This creates a row. You can use the following steps to re-create this table within the BODY of one of your web pages:

  1. Type <TABLE> to let the browser know that you are opening a table.
  2. On the next line, Type <TR> (it stands for Table Row) to start a new row.
  3. For every cell you want in that row:
    1. Open a new cell by typing <TD> (it stands for Table Data)
    2. Type in whatever you want to appear in that cell.
    3. Close the cell by typing </TD>
  4. Type </TR> to close the row.
  5. Repeat steps 2-4 for every row.
  6. Type </TABLE> to close the table.

That's all it takes to make a table. Your browser will count up the number of rows and the number of columns in the largest row and make a table of the appropriate size. Just remember that in the final table, every row will have as many columns as the largest row regardless of how many you typed in. Extra cells will just appear blank.

Three Basic Tags

As you saw above, there are only three basic tags you need to know. Here are a few more details about each of them:

The <TABLE> tag

Every table must be surrounded by the <TABLE> and </TABLE> tags. Whenever your browser encounters a table, it pauses to read in everything between these two tags before deciding how to arrange and display it. If you forget either one of them, your table will not show up at all!

The <TABLE> tag has quite a few attributes. You never need to include any of them, but here are a few of the more useful ones:

Attribute Description
BORDER Tables, by default, have no visible borders. If you would like your table to have a border, assign this attribute to a value (in pixels). BORDER=1 (or just BORDER) will give your table a very thin border. BORDER=5 is much thicker.
CELLSPACING This attribute controls how much space (in pixels) there is between cells. Default cellspacing is 1, but you can use this attribute to raise or lower it (e.g. CELLSPACING=0).
CELLPADDING Cellpadding is the space between a cell's borders and content. In tables with borders, you may want to set CELLPADDING=5 to avoid having text touch the edges of the cells.
WIDTH WIDTH can be assigned either a number of pixels or a percentage of the width of the window. For example, WIDTH=80% will result in a table that is always 80% as wide as the browser window. WIDTH=80 will result in a table that is always 80 pixels wide.
BGCOLOR The BGCOLOR attribute controls the default background color of every cell in your table. As with the BGCOLOR attribute in the <BODY> tag, BGCOLOR can only be assigned a built-in color or RGB value (e.g. BGCOLOR="blue"). If no BGCOLOR is specified, the table will have the same background color as the page.

The <TR> tag

Every row you create within your table must be surrounded by <TR> and </TR> tags. Here are some of the attributes you can include:

Attribute Description
ALIGN This attribute will control the horizontal alignment in the text of each cell in that row. Its default is LEFT, but you can set it to CENTER or RIGHT.
VALIGN This attribute will control the vertical alignment in the text of each cell in that row. Its default is MIDDLE, but you can set it to TOP or BOTTOM.
BGCOLOR The BGCOLOR attribute controls the default background color of every cell in the row. As with the BGCOLOR attribute in the <TABLE> tag, BGCOLOR can only be assigned a built-in color or RGB value (e.g. BGCOLOR="blue"). If no BGCOLOR is specified, the row will have the same background color as the table.

The <TD> tag

All text within your table should be surrounded by <TD> (or <TH>) and </TD> (or </TH>). These tags create the actual data cells, and thus have a lot of optional attributes to help you customize your table:

Attribute Description
ALIGN This attribute will control the horizontal alignment in the text in the cell. Its default is LEFT, but you can set it to CENTER or RIGHT.
VALIGN This attribute will control the vertical alignment in the text in the cell. Its default is MIDDLE, but you can set it to TOP or BOTTOM.
BGCOLOR The BGCOLOR attribute controls the background color of this cell. As with the BGCOLOR attribute in the <TABLE> tag, BGCOLOR can only be assigned a built-in color or RGB value (e.g. BGCOLOR="blue"). If no BGCOLOR is specified, the cell will have the same background color as the row.
ROWSPAN

ROWSPAN controls how many rows the cell occupies, allowing you to merge the cells of three rows into one tall cell. The default is 1, but you can raise this attribute to be anything up to the number of remaining rows. For example, ROWSPAN=3 will cause the cell to be 3 rows high.

When you have set a cell to occupy more than one row, you will usually be filling fewer cells in the next few rows. If, for example, you set ROWSPAN=4 on one cell in a table that is 5 columns wide, the next 3 rows would have 4 columns to fill instead of 5.

COLSPAN COLSPAN controls how many columns the cell occupies, allowing you to merge the cells of three columns into one long cell. The default is 1, but you can raise this attribute to be anything (if you raise it above the number of remaining columns, it will create new ones on the right). For example, COLSPAN=3 will cause the cell to be 3 rows long.

Other table tags

There are two other tag pairs which can be used within tables:

Advanced Example

Here is a sample table created to display a schedule:

Schedule

Time Monday Tuesday Wednesday Thursday Friday
9:00 MAT 104 MAT 104 MAT 104
10:00 PHI 201 PHI 201
11:00 COS 111 COS 111 COS 111
Noon L U N C H

Notice that it uses many of the attributes we described above, especially ROWSPAN and COLSPAN. The HTML code that created this table is listed below. If you examine this code closely, you'll notice that the fourth row (11:00) seems to be missing a few cells. This is because the third row has two cells with ROWSPAN=2 which means they each automatically take up a cell in the fourth row as well.

HTML code for Schedule
<TABLE BORDER=1 CELLPADDING=5>

    <CAPTION><H3>Schedule</H3></CAPTION>

    <TR BGCOLOR=SILVER>
	<TH BGCOLOR=BEIGE>Time</TH>
	<TH>Monday</TH>
	<TH>Tuesday</TH>
	<TH>Wednesday</TH>
	<TH>Thursday</TH>
	<TH>Friday</TH>
    </TR>

    <TR ALIGN=CENTER VALIGN=MIDDLE>
	<TD BGCOLOR=BEIGE>9:00</TD>
	<TD>MAT 104</TD>
	<TD></TD>
	<TD>MAT 104</TD>
	<TD></TD>
	<TD>MAT 104</TD>
    </TR>
    <TR ALIGN=CENTER VALIGN=MIDDLE>
	<TD BGCOLOR=BEIGE>10:00</TD>
	<TD></TD>
	<TD ROWSPAN=2>PHI 201</TD>
	<TD></TD>
	<TD ROWSPAN=2>PHI 201</TD>
	<TD></TD>
    </TR>

    <TR ALIGN=CENTER VALIGN=MIDDLE>
	<TD BGCOLOR=BEIGE>11:00</TD>
	<TD>COS 111</TD>
	<TD>COS 111</TD>
	<TD>COS 111</TD>
    </TR>

    <TR ALIGN=CENTER VALIGN=MIDDLE>
	<TD BGCOLOR=BEIGE>Noon</TD>
	<TD COLSPAN=5 BGCOLOR=TURQUOISE>
	    L U N C H</TD>
    </TR>

</TABLE>

A table of your own...

  1. Create a table to display your schedule on a new web page called "lab3.html". Be sure to save this new file in your public_html directory. You can view it at "http://www.princeton.edu/~yourname/lab3.html".
    BEFORE YOU DO ANYTHING ELSE, MAKE SURE THAT YOU HAVE SAVED THE INFORMATION AND CAN ACCESS THIS PAGE. Throughout the lab, be sure that your changes are being saved on your web page; you don't want to lose your work. And make sure that the page is accessible to others as well; check with one of the lab assistants or get a friend to look at it.

    For now, don't try to use COLSPAN and ROWSPAN in your table. Instead, just repeat the cell multiple times like this:

    Time Monday Tuesday Wednesday Thursday Friday
    9:00 MAT 104 MAT 104 MAT 104
    10:00 PHI 201 PHI 201
    11:00 COS 111 PHI 201 COS 111 PHI 201 COS 111
    Noon L U N C H L U N C H L U N C H L U N C H L U N C H

  2. FOR EXTRA CREDIT: Alter your schedule table to have half-hour increments instead of hourly increments. Then use ROWSPAN and COLSPAN to merge adjacent identical cells. (Your schedule must cover at least 6 hours and 5 days)


Part 2: Forms

Introduction to Forms

As you have explored the web, you have certainly encountered forms in some way or another. Search engines, guest books, comment pages and online surveys are all different kinds of forms. In fact, any time a Web page asks you for information which is used to customize the page, you have most likely used a form.

Forms are basically collections of data entry fields like checkboxes, radio buttons, text boxes, etc. that ask for information from visitors to a page. When the visitor presses a "submit" button, all of that information is then collected and used by a program for some purpose. Search Engines take keywords and generate lists of relevant pages. Guestbooks take entries and update a long HTML page. Comment forms usually send the comments to administrators via email.

Today, you will learn how to create a form which collects some sort of information from the people who visit your web page and then sends it to you via email. Later in the course, when you learn a programming language, you may be able to write programs to interpret the collected data instead of just sending it to you.

What's in a form?

Here's a sample form that shows all of the different form components you can use:
Text Field: Try typing in this password box:
Check out this checkbox:

RadioButton 1:
RadioButton 2:
RadioButton 3:

The <FORM> tag

Just as tables must be surrounded by <TABLE></TABLE>, everything in a form must go between <FORM> and </FORM>.

The <FORM> tag has several mandatory attributes:

Thus your form should always begin with:

<FORM METHOD="post" ACTION="http://www.princeton.edu/cgi-bin/Princeton/mailform?userid@princeton.edu">

(Replace userid with your User ID and type the entire ACTION tag on one line.) and end with:

</FORM>

Adding components

Once you've opened your form, you can begin interspersing form elements with your HTML. All of the regular HTML tags will still work, and you can also add text fields, text areas, select menus, checkboxes, radio buttons and more.

We're going to give a quick explanation of how to use each item and then ask you to create a form on your web page that lets visitors sign in and tell you about themselves.

Selection Boxes

Selection boxes let your visitors choose one option from a drop-down menu, like this:

It is easy to see the uses that such an item could have. Here is the HTML code that created it:

What you write in HTML How it looks on your web page
<SELECT NAME="gender">
<OPTION> Male
<OPTION> Female
<OPTION> Undecided
</SELECT>

To begin, we used the <SELECT> tag. Notice the NAME attribute inside it. It is very important to give every form component a name using the NAME attribute. Whenever someone submits your form, you will receive an email listing all of their answers. For example, if you selected "Male" above and submitted the form, the email would include "gender=Male". If you don't name your components, then you won't know which answer goes with which question!

Next, we listed all of the options available. Although it is not necessary, you should probably list each option on a separate line to make the HTML more readable. Your selection box can have as many or as few options as you wish, as long as each is preceded by the <OPTION> tag.

Finally, when we were done with the options, we used the </SELECT> tag to close the selection box.

Text Areas

Text Areas let your visitors type in many lines of text, like this:

Here's the HTML code to create such a text area:

What you write in HTML How it looks on your web page
Questions/Comments:<BR>
<TEXTAREA NAME="comments">
I love this Web site!
</TEXTAREA>
Questions/Comments:

To create the text area, we first used the tag <TEXTAREA> to open it. As usual, we included the NAME attribute with a descriptive name that would help us identify it later.

Next, we typed in what we wanted to appear inside the text area. Since text areas are primarily for collecting information, you probably won't ever need to put anything inside and can omit that part. However, if you do want something to appear in the text area, note that each line of text you type appears on a separate line in the text area.

Finally, we typed </TEXTAREA> to close the text area.

The size of a text area can be set using the ROWS and COLS attributes to specify how many lines of text and how many characters per line should be displayed. For example, our original text area above can be made larger by using <TEXTAREA ROWS=4 COLS=30>

Text Fields

Text Fields are small, one-line text boxes which are useful for collecting information like names, email addresses, phone numbers, etc. Here is an example, along with the code used to generate it:

What you write in HTML How it looks on your web page
Email:
<INPUT TYPE = "text" NAME="email" SIZE=15 VALUE="type name here">
Email:

Text Fields, along with the rest of the components we'll cover, use the <INPUT> tag. Since the same tag is used for buttons, checkboxes and other components, however, we need to specify a type in the TYPE attribute. For text fields, that type is "text".

As usual, we gave our component a descriptive name, but there are two other attributes we may also wish to set:

Password Fields

Password fields are just like text fields, except that, for security reasons, every letter you type in one shows up as an asterisk. This prevents strangers from looking over your shoulder and discovering your password as you type it. When the form is submitted, the actual text typed will show up in the email next to the NAME of the password field.

What you write in HTML How it looks on your web page
Secret Code:
<INPUT TYPE = "password" NAME="secret" SIZE=5>
Secret Code:

Try typing in the password field above and notice the effect. You will probably not need to use this component very frequently, but if you do, you may use any of the attributes of text fields in the same way that you would use them for text fields.

Checkboxes

Check boxes are exactly what they sound like -- little boxes which can be checked and unchecked by clicking on them. There are two different ways in which to use checkboxes in forms, however. The first example here is used for a group of unrelated questions:

What you write in HTML How it looks on your web page
I am a student:
<INPUT TYPE = "checkbox" NAME="student">
<BR>
I drive a car:
<INPUT TYPE = "checkbox" NAME="car" CHECKED>
I am a student:
I drive a car:

In this case, you want a separate answer for each checkbox question. Each has its own name and when the form is submitted, each will each only be listed if it is checked. Notice that including the attribute CHECKED (without a value) in the second item's tag causes it to start out checked.

You can also use checkboxes to let visitors selected more than one option from a list. Look at this example:

What you write in HTML How it looks on your web page
Which classes have you taken:<BR>
<INPUT TYPE = "checkbox" NAME="classes" VALUE = "mat103">
MAT 103 <BR>
<INPUT TYPE = "checkbox" NAME="classes" VALUE = "eng101">
ENG 101<BR>
<INPUT TYPE = "checkbox" NAME="classes" VALUE = "cos109">
COS 109<BR>
<INPUT TYPE = "checkbox" NAME="classes" VALUE = "phi201">
PHI 201
Which classes have you taken:
MAT 103
ENG 101
COS 109
PHI 201

In this case, the email submission would group all of these together under the name "classes". Instead of receiving a separate answer such as "mat103 = on" for each of them, you would get something like "classes = mat103, phi201".

As with the above example, you can cause any of these to start out checked by including the CHECKED attribute.

Radio Buttons

Radio Buttons are a lot like checkboxes, except that only one can be selected at a time. Just like station buttons on old radios, when you push one, the one that was pushed before pops out. Try out this example:

What you write in HTML How it looks on your web page
I like ice cream:<BR>
<INPUT TYPE = "radio" NAME="ice cream" VALUE = "lots">
A lot<BR>
<INPUT TYPE = "radio" NAME="ice cream" VALUE = "some">
Somewhat<BR>
<INPUT TYPE = "radio" NAME="ice cream" VALUE = "not">
Not at All
I like ice cream:
A lot
Somewhat
Not at All

Every group of radio buttons must share the same name. In your email, you will get one answer for that name which tells you which radio button was selected (e.g. "ice cream = lots" or "ice cream = some").

Reset Buttons

Reset Buttons, when pushed, reset all the answers in a form to their original default values. Usually, one reset button will be placed at the end of a form, right next to the submit button. Here is an example of a reset button in a small form (with only one other component).

What you write in HTML How it looks on your web page
Please enter a problem:<BR>
<INPUT TYPE = "text" NAME="problem">
Press this button to make it go away:<BR>
<INPUT TYPE = "reset" VALUE = "Poof">
Please enter a problem:

Press this button to make it go away:

Don't be fooled by this example. Reset buttons will reset the values of every component in the same form (between the same set of <FORM></FORM>), not just one item.

The attribute VALUE is optional and will set the text message on the button. If you don't specify a VALUE, the button will usually just say "Reset".

Submit Buttons

The last form component we will show you is the Submit Button. It looks just like the Reset Button, but instead of resetting all of the answers, it submits them. When a visitor presses the submit button, all of the answers within the same form are collected and passed on to whatever program was specified in the ACTION attribute of the <FORM> tag.

Below is a Submit Button with no other components. This one doesn't actually do anything, but we've provided this example so that you will know what it looks like and how to include one in your forms.

What you write in HTML How it looks on your web page
This is a Submit Button:<BR>
<INPUT TYPE = "submit" VALUE = "Submit Everything">
This is a Submit Button:

As with Reset Buttons, the attribute VALUE is optional and will set the text message on the button. If you don't specify a VALUE, the button will usually just say "Submit" or "Submit Query".

Creating a Sign-In form for your Web page

Now that you have learned how to use each component, we'd like you to combine them all by creating a Sign-In form for your Web page. Basically, this is a form, like the one at the bottom of this page, which asks each visitor for his name, email address and other interesting information. Once you're done, you'll begin receiving email about everyone who visits your Web site!

  1. Create a Sign-In form on your "lab3.html" page with at least 5 data entry fields (places to input answers), and using at least 3 different kinds of components. Before you start, check out the tips and example below, but please do not copy the sample form we've provided.
  2. Once you've finished your form, load the page and try it out (check your email to see if you receive the correct message). You will be graded on this, so make sure it works.

While you work, keep the following tips in mind:

Here's a quick example to get you started. It uses a table to make an attractive little Sign-In Form:

What you write in HTML

<TABLE BORDER=5 BGCOLOR="silver">
 <TR ALIGN=CENTER>
  <TD>
   <FORM METHOD="post" ACTION="http://www.princeton.edu/cgi-bin/Princeton/mailform?userid@princeton.edu">
    <B>Name:</B> 
    <INPUT TYPE="text" NAME="name"><BR><BR>
    <B>Email:</B> 
    <INPUT TYPE="text" NAME="email"><BR><BR>
    <B>Please rate this page:</B><BR>
     <INPUT TYPE = radio NAME=rate VALUE=poor>
     Poor
     <INPUT TYPE = radio NAME=rate VALUE=ok>
     OK
     <INPUT TYPE = radio NAME=rate VALUE=good>
     Good
    <BR><BR>
    <INPUT TYPE="submit">
    <INPUT TYPE="reset">
    </FORM>
   </TD>
  </TR>
</TABLE>
How it looks on your Web page
Name:

Email:

Please rate this page:
Poor OK Good


Follow this link for Part 3: Frames