HTML: Creating a Web Based Form

Forms consist of a form tag and input elements. The input elements is where the user will enter information. Input elements take the shape of text input boxes, radio buttons, check boxes and drop down lists or menus. All the input elements in a form are contained within form tags.

We'll get into the specific input tags in a moment. For now let's just look at the Form tag.

<form>

input elements go here

</form>


Form tags for a real functioning form will have three attributes.

METHOD Takes one of two arguments. Either Post or Get
ACTION This takes the form of a URL where the information from the form will be processed
NAME The name of the form. This is important to allow JavaScript awareness of the entire form

Here is an example of a Form Tag

<form method="post" action="myactionscript.php" name="myForm">

METHOD
<form method="post" action="myactionscript.php" name="myForm">

To Post or to Get, that is the Question

The Get Method adds the information from the form to the action web pages URL. This will be visible within the address bar of the browser window.
Try This:
Enter Your Name What Day is it?
In the window that opened you should have seen something like this.

?nameField=Doug+Johnson&dayField=Thursday&Submit=Submit

This is referred to as a Query String. The query string begins at the end of the URL where the question mark is. You've probably noticed one before when using search engines or shopping on-line. A URL with a Query string can be bookmarked. If you were shopping for an item and located it using a search, a bookmarked URL with Query string would bring you back to that particular item. On-Line retailers like this.

So what if you don't want the user to see what is being sent within the address? Use Post. Post conceals the information from the user by placing the data in the message body - not in the URL.

Read more about it: URIs, Addressability, and the use of HTTP GET and POST

ACTION
<form method="post" action="myactionscript.php" name="myForm">

As mentioned previously, real working forms would be submitted to a web server. The true processing of the form information is done on the web server. The value of the action attribute is the URL where the programming code exists on the server to handle your form data. The code would receive the data and process it accordingly.

The action attribute may also take the form of an email address. This will trigger the users default email applciation to send the form data. This method is not always reliable however. To ensure the form data arrives in a readbale state, add the following attribute/value pair: enctype="text/plain"

Here is what an email action would look like:
<FORM METHOD="post" ACTION="mailto:dougj@digitalvertebrae.com" name="myForm" enctype="text/plain">

Name
<form method="post" ACTION="myactionscript.php" name="myForm">

The name attribute allows you to identify the form on the web page. This can be important if you have more than one form on a page or need to access the form with JavaScript. As you have seen previously, you should name the form meaningfully. If your form is a survey about favorite dog breeds, name it "favorite breeds".

Input Elements
The information sent to the server for processing comes from the Input Elements. The data they carry consists of the name of the input element and the value of the data of the input element.

Text Field
Text Fields used generally for name and address information or anything that the programmer can not readily predefine. The name argument can be anything you choose. It should however relate to the information that it is requesting. For example - city. Additional attributes include size and maxlength.
City:
City: <input type="text" name="city" value="type here">

Password Field
Exactly the same as the above text field, only that the input is obscured from the user.
Password:
Password: <input type="password" name="user_password"">

Text Area Input Field
Similar to what a text field does, but allows for multiple lines of information. The text that appears between the <textarea> </textarea> tags becomes the value of this input element. The cols attribute defines how wide the field will appear. The rows attribute defines how high the field will appear.
Comments:
Comments: <br><textarea name="comments" cols="40" rows="10"></textarea>

Checkboxes
Check boxes allow the user to make multiple choices. You will notice in the sample HTML below, all the checkboxes have the same name. By naming them the same, they will belong to the same group. If I were to name three check boxes "ice-cream" with each having a respective value of "Chocolate", "Vanilla", and "Strawberry", the form would submit ice-cream = Chocolate, Strawberry - where the user had selected those two flavors.
Favorite Ice Cream Flavors:
Chocolate
Strawberry
Vanilla
Favorite Ice Cream Flavors: <br>
<input type="checkbox" name="ice-cream" value="chocolate">
Chocolate<br>
<input type="checkbox" name="ice-cream" value="strawberry">
Strawberry<br>
<input type="checkbox" name="ice-cream" value="vanilla">
Vanilla

Radio Buttons
Radio buttons only allow for the user to make a single choice. Keeping the names the same is important here. For any question, the user should only be able to select one answer.
9 x 5 =
35
40
45
9 x 5 =<br>
<input type="radio" name="9x5" value="35">
35<br>
<input type="radio" name="9x5" value="40">
40<br>
<input type="radio" name="9x5" value="45">
45

Select Menu
Menus give the user a variety of choices in response to a question in the form of a drop down menu. This is simpler to put together than a long array of check boxes. The actual tag for Select Menus and Select List is the same <select>. To change form a Menu to a list, a couple of other parameters are added (see below). After the select tag, we add our options, each having it's own specific value. After all options have been defined, we close off with a close </select> tag. By default, the field will initially be blank. We can specify an actual value by adding the keyword selected within the option tag. This is the case with Gimli in the example below.
Where do you reside in Manitoba?

<select name="residence">
<option value="Winnipeg">Winnipeg</option>
<option value="Brandon">Brandon</option>
<option value="Gimli" selected>Gimli</option>
<option value="Flin Flon">Flin Flion</option>
<option value="Selkirk">Selkirk</option>
<option value="Steinbach">Steinbach</option>
<option value="Other">Other</option>
</select>

Select List
To change a menu to a list, we add a size attribute. In the example below, the size is set to seven so that we can see all possible options. We have given the user the option to select more than one item by adding the attribute multiple to the select tag as well. If there are more items to be displayed beyond what the size is set to, scroll bars will appear within the list window.
Where do you reside in Manitoba?

<select name="residence" size="7" multiple>
<option value="Winnipeg" selected>Winnipeg</option>
<option value="Brandon">Brandon</option>
<option value="Gimli">Gimli</option>
<option value="Flin Flon">Flin Flion</option>
<option value="Selkirk">Selkirk</option>
<option value="Steinbach">Steinbach</option>
<option value="Other">Other</option>
</select>

Submit Button
Buttons are the final element we will look at. Essentially these buttons are triggers that will effect the form in different ways The Submit button will send the data from the form on its way to the server.
<input type="submit" value="Submit">

Reset Button
Buttons are the final element we will look at. Essentially these buttons are triggers that will effect the form in different ways The Submit button will send the data from the form on its way to the server.
<input type="reset" value="Reset">

A Complete Form
Now that we have looked at the various form input elements, let's look at a complete form. Notice that I have placed the form elements in table cells seperate from the questions themselves. This helps keep the form neat and organized.
Web Development: an introduction
student registration
First Name:
Last Name:
   
Address:
 
   
City:
   
Province:
   
Email:
Password:
<form name="student_registration" method="post" action="mailto:dougj@digitalvertebrae.com" enctype=text/plain" >
<table width="100%" border="1" cellpadding="0" cellspacing="2" class="bodyText">
<tr align="center">
<td colspan="2"><strong>Web Development: an introduction<br>
student registration </strong></td>
</tr>
<tr>
<td width="25%">First Name:</td>
<td width="75%"><input name="firstname" type="text" id="firstname" size="20" maxlength="15"></td>
</tr>
<tr>
<td width="25%">Last Name:</td>
<td><input name="lastname" type="text" id="lastname" size="20" maxlength="15"></td>
</tr>
<tr>
<td width="25%">&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td width="25%">Address:</td>
<td><input name="address1" type="text" id="address1" size="30" maxlength="25"></td>
</tr>
<tr>
<td width="25%">&nbsp;</td>
<td><input name="address2" type="text" id="address2" size="30" maxlength="25"></td>
</tr>
<tr>
<td width="25%">&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td width="25%">City:</td>
<td><input name="city" type="text" id="city" size="20" maxlength="15"></td>
</tr>
<tr>
<td width="25%">&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td width="25%">Province:</td>
<td><select name="province" id="province">
<option value="Ontario">Ontario</option>
<option value="Manitoba" selected>Manitoba</option>
<option value="Saskatchewan">Saskatchewan</option>
<option value="Alberta">Alberta </option>
</select></td>
</tr>
<tr>
<td width="25%">&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td width="25%">Email:</td>
<td><input name="email" type="text" id="email" size="30" maxlength="25"></td>
</tr>
<tr>
<td>Password:</td>
<td><input name="password" type="password" id="password" size="10" maxlength="8"></td>
</tr>
<tr>
<td></td>
<td align="right"><input type="reset" value="Reset">
<input type="submit" value="OK"></td>
</tr>
</table>
</form>

created: 2006-11-05
Google
web www.digitalvertebrae.com
Exercises
The exercises for this week are listed below and available in PDF or MS Word formats.
HTML - Web Input Form
 
©2009 Web Site Development: an introduction