Lesson 4

Forms

OBJECTIVE

To learn how to create forms and make them active to the user for filling out information

PREREQUISITE

ADVANCED BLOCK & INLINE ELEMENT TECHNIQUES


Here we will examine the process of making forms using tags for which you should be familiar with now.

FORM FUNCTION

The function of the form is to collect information from the user and preserve the data for later use. An example of this form is, Registration Information which includes the following: name, email, address, credit card, etc. Form elements are like text fields, textarea fields, drop-down menus, radio buttons, checkboxes, etc. which are used to take from the user.


SIMPLE SYNTAX FOR USING <form> IS AS FOLLOWS:

 <form action="back-end script" method="posting method">
form elements like input, textarea etc.
</form>

Most used attributes are:

name: This is the name of the form.

action: Here you will specify any script URL which will receive uploaded data.

method: Here you will specify method to be used to upload data. It can take various values but most frequently used are GET and POST.

target: It specifies the target page where the result of the script will be displayed. It takes values like _blank, _self, _parent etc.

enctype: You can use the enctype attribute to specify how the browser encodes the data before it sends it to the server. Possible values are like:

application/x-www-form-urlencoded - This is the standard method most forms use. It converts spaces to the plus sign and non-alphanumeric characters into the hexadecimal code for that character in ASCII text.

mutlipart/form-data - This allows the data to be sent in parts, with each consecutive part corresponding the a form control, in the order they appear in the form. Each part can have an optional content-type header of its own indicating the type of data for that form control.

name: This is the name of the form.


There are different types of form controls that you can use to collect data from a visitor to your site.

Text input controls

Buttons

Checkboxes and radio buttons

Select boxes

File select boxes

Hidden controls

Submit and reset button


HTML Forms - Text Input Controls:

There are actually three types of text input used on forms:

Single-line text input controls: Used for items that require only one line of user input, such as search boxes or names. They are created using the <input> element.

Password input controls: Single-line text input that mask the characters a user enters.

Multi-line text input controls: Used when the user is required to give details that may be longer than a single sentence. Multi-line input controls are created with the <textarea> element.


SINGLE-LINE TEXT IMPUT CONTROLS

<form action="/cgi-bin/hello_get.cgi" method="get">
First name:
<input type="text" name="first_name" />
<br>
Last name:
<input type="text" name="last_name" />
<input type="submit" value="submit" />
</form>

The following is a list of attributes for <input> tag.

type: Indicates the type of input control you want to create. This element is also used to create other form controls such as radio buttons and checkboxes.

name: Used to give the name part of the name/value pair that is sent to the server, representing each form control and the value the user entered.

value: Provides an initial value for the text input control that the user will see when the form loads.

size: Allows you to specify the width of the text-input control in terms of characters.

maxlength: Allows you to specify the maximum number of characters a user can enter into the text box.


Here is a basic single-line password input code to use user password:

<form action="/cgi-bin/hello_get.cgi" method="get">
Login :
<input type="text" name="login" />
<br>
Password:
<input type="text" name="password" />
<input type="submit" value="submit" />
</form>

Full Form Example:

My Form
Part 2
Your choices are: or


QUIZ 4

Now lets dig a little deeper into the coding world and see what you have learned.

⇑ Back To The Top ⇑

Give It A Try!