Tables and Forms
Tables and Forms
Objectives
• Use tables to structure a webpage
• Use forms to collect user input
– Method:Get, Post
– Input
– Select
– Textarea
In Class
• Keep 2 browser windows open.
• Try examples as we go along.
• Save notepad as html to desktop.
• (Double) click to see.
Tables
• To structure the page
two rows
Headings
• HTML provides the <th> tag for table
headings
• Text formatted with the <th> tag is
centered within the cell and displayed in a
boldface font
• The <th> tag is most often used for
column headings, but you can use it for
any cell that you want to contain centered
boldfaced text
Table Caption
• The syntax for creating a caption is: <caption
align=“alignment”>caption
text</caption>
– alignment indicates the caption placement
– a value of “bottom” centers the caption below
the table
– a value of “top” or “center” (newer) centers the
caption above the table
– a value of “left” or “right” (newer) place the
caption above the table to the left or right
Table
• <table>...</table> enclose a table
• align
• bgcolor
Cell Width
• To set the width of an individual cell, add
the width attribute to either the <td> or
<th> tags
• The syntax is: width=“value”
– value can be expressed either in pixels or
as a percentage of the table width
– a width value of 30% displays a cell that is
30% of the total width of the table
Cell Height
• The height attribute can be used in the <td>
or <th> tags to set the height of individual
cells
• The height attribute is expressed either in
pixels or as a percentage of the height of the
table
• If you include more text than can be
displayed within that height value you specify,
the cell expands to display the additional text
Spanning Rows and Columns
• To merge several cells into one, you need to
create a spanning cell
• A spanning cell is a cell that occupies more than
one row or column in a table
• Spanning cells are created by inserting the
rowspan and colspan attribute in a <td> or
<th> tag.
• The syntax for these attributes is:
<td rowspan=“value” colspan=“value”> …
</td>
– value is the number of rows or columns that the
cell spans in the table
Spanning Rows and Columns
• When a cell spans several rows or
columns, it is important to adjust the
number of cell tags used in the table row
• When a cell spans several rows, the rows
below the spanning cell must also be
adjusted
Spanning Rows and Columns
•
This cell
spans three
columns
This cell
spans three
rows
Row-Spanning Cell
•
four table
cells in the
first row
HTML code
resulting table
Aligning Contents
• By default, cell text is placed in the middle
of a cell, aligned with the cell’s left edge
• You can specify a different horizontal
alignment for a <td> or <th> element
align=“position”
position is left, center or right
Attributes <td>
• align – Horizontal alignment
• valign – Vertical alignment (top, middle,
bottom)
• width – Width of cell in pixels or
percentage of table width
• height – Height of cell in pixels or
percentage of table height
Attributes <td>
• colspan – Makes cell take up space of one
or more cells
• rowspan – Makes cell take up space of
one or more rows
• bgcolor
Placeholder
• Insert a no-breaking space in the empty cell.
<tr>
<td>Row1 Col1</td>
<td>Row2 Col2</td>
</tr>
<tr>
<td> </td>
<td>Row2 Col2</td>
</tr>
Nested Tables
• Tables can be created within the cell of
another table.
• Carefully work on one table at a time.
Examples and homework
• See simple_calculator.html
• See table_example.html
• See form_border.html
• Do table assignment
HTML Forms
• http://www.ncsa.uiuc.edu/SDG/Software/M
osaic/Docs/fill-out-forms/overview.html
• Summary of HTML tags for forms and
examples of simple forms
• www.webcom.com/html/tutor/forms
• Forms tutorial
HTML Forms
• HTML Forms were the first way for users
to interact with the Web
– controls (inputs)
– form submission
– form processing
• Faster
• More accurate
• Cheaper
• Virus free
• GET -- this is the default method and
causes the fill-out form contents to be
appended to the URL.
• POST -- this method causes the fill-out
form contents to be sent to the server in a
data body rather than as part of the URL.
• ENCTYPE specifies the encoding for the
fill-out form contents. This attribute applies
only if METHOD is set to POST
Form Attributes
• Name
• Method
– get -- this is the default method and causes
the fill-out form contents to be appended to
the URL. Use only when submit has no effect.
e.g. pure query.
– post -- this method causes the fill-out form
contents to be sent to the server in a data
body rather than as part of the URL.
Form Attributes
• Action :
– The URL to which the data is sent (may be
local) or
– mailto: so the data just gets sent in an email.
• Enctype: the encoding for the fill-out form
contents. This attribute applies only if the
method is post.
Form Examples
<form name=“data” method=“post”
action=“http://www.site.com/cgi-bin/myprog”>
<form method=“get” action=“/cgi-bin/myprog”>
<form method=post enctype=“text/plain”
action=“mailto:[email protected]”>
<input type="text" name=“user" size="25"><br/>
<input type="submit" value="submit">
<input type="reset" value="reset">
</form>
Form Contents
• A Form may contain anything except
another Form.
• Interface elements (controls):
– input tags
– select tags
– textarea tags
Input Attributes
The attributes to INPUT are as follows:
TYPE must be one of:
"text" (text entry field; this is the default)
"password" (text entry field; entered characters are represented as asterisks)
"checkbox" (a single toggle button; on or off)
"radio" (a single toggle button; on or off; other toggles with the same NAME are grouped into "one of many" behavior)
"submit" (a pushbutton that causes the current form to be packaged up into a query URL and sent to a remote server)
"reset" (a pushbutton that causes the various input elements in the form to be reset to their default values)
NAME is the symbolic name (not a displayed name -- normal HTML within the form is used for that) for this input field.
This must be present for all types but "submit" and "reset", as it is used when putting together the query string
that gets sent to the remote server when the filled-out form is submitted.
VALUE, for a text or password entry field, can be used to specify the default contents of the field. For a checkbox or a
radio button, VALUE specifies the value of the button when it is checked (unchecked checkboxes are disregarded
when submitting queries); the default value for a checkbox or radio button is "on". For types "submit" and "reset",
VALUE can be used to specify the label for the pushbutton.
CHECKED specifies that this checkbox or radio button is checked by default; this is only appropriate for checkboxes
and radio buttons.
SIZE is the physical size of the input field in characters; this is only appropriate for text entry fields and password entry
fields. If this is not present, the default is 20. Multiline text entry fields can be specified as SIZE=width,height; e.g.
SIZE=60,12. Note: the SIZE attribute should not be used to specify multiline text entry fields now that the
TEXTAREA tag is available.
MAXLENGTH is the maximum number of characters that are accepted as input; this is only appropriate for text entry
fields and password entry fields (and only for single-line text entry fields at that). If this is not present, the default
will be unlimited. The text entry field is assumed to scroll appropriately if MAXLENGTH is greater than SIZE.
Input: type attribute
• <input type=“submit”> (submits data)
• <input type=“reset”> (resets to defaults)
• <input type=“button”> (user defined)
• <input type=“text”>
– <input type=“password”> (asterisks)
• <input type=“checkbox”> (toggle)
• <input type=“radio”> (toggle-only one)
• <input type=“hidden”> (isn’t displayed)
Input: type attribute
• Default is “text”
Other Input Attributes
• name
• value
• checked
• size
• maxlength
Submit Button
• When a user clicks on a submit button the
browser submits the contents of all other
fields to a web server using the method
and action attributes.
• <input type=submit value=“Send now">
• May have multiple submit buttons on a
page (with different values).
• Default value is “Submit Query”
Reset Button
• When user presses the reset the entire
form is cleared.
• The displays are reset to the initial values
which have been saved.