HTML Form Lecture Notes
HTML Form Lecture Notes
HTML Forms
(i) An HTML form is a section of a HTML page containing normal content and special
elements called controls (checkboxes, radio buttons, menus, etc.), and labels on those
controls. Users generally "complete" a form by modifying its controls (entering text,
selecting menu items, etc.), before submitting the form to an agent for processing (e.g., to
a Web server, to a mail server, etc.).
(ii) A web form or HTML form on a web page allows a user to enter data that is sent to a
server for processing. Forms can resemble paper or database forms because web users fill
out the forms using checkboxes, radio buttons, or text fields. For example, forms can be
used to enter shipping or credit card data to order a product, or can be used to retrieve
search results from a search engine.
HTML tags
A form can contain input elements like text fields, checkboxes, radio-buttons, password fields
etc. A form can also contain select lists and textarea elements.
< form>
input elements
…..
< /form>
The most important form element is the input element. The input element is used to select user
information. An input element can vary in many ways, depending on the type attribute. An input
element can be of type text field, checkbox, password, radio button, submit button, and more.
1. Text Fields
<input type="text" /> defines a one-line input field that a user can enter text into.
Example
< form>
<label for=”First name”>First name:</label>< input type="text" name="firstname" /><br />
label for=”Last name”>last name:</label>Last name:< input type="text" name="lastname"
size=”10” />
< /form>
First name:
Last name:
Note:-
(i) SIZE attribute determines the size of the textbox in characters. Default = 20 characters
(ii) MAXSIZE attribute: determines the maximum number of characters that the field will
accept.
2. Password Field
Example
< form>
Password:< input type="password" name="pwd" />
< /form>
Password:
Note: The characters in a password field are masked (shown as asterisks or circles).
3. Radio Buttons
<input type="radio" /> defines a radio button. Radio buttons let a user select ONLY ONE of a
limited number of choices.
Example
< form>
< input type="radio" name="sex" value="male" /> Male<br />
< input type="radio" name="sex" value="female" /> Female
< /form>
Male
Female
4. Checkboxes
<input type="checkbox" /> defines a checkbox. Checkboxes let a user select ONE or MORE
options of a limited number of choices.
Example:
< form>
< input type="checkbox" name="vehicle" value="Bike" /> I have a bike<br />
< input type="checkbox" name="vehicle" value="Car" /> I have a car
< /form>
I have a bike
I have a car
5. Submit Button
A submit button is used to send form data to a server. The data is sent to the page specified in the
form's action attribute. The file defined in the action attribute usually does something with the
received input:
Example:
< form>
Username:< input type="text" name="user" />
< button type="submit" value="Submit"> Submit</button>
< /form>
Submit
Username:
If you type some characters in the text field above, and click the "Submit" button, the browser
will send your input to a page called "html_form_action.asp". The page will show you the
received input.
A dropdown menu is a toggleable menu that allows the user to choose one value from a
predefined list.
Example:
Note: The <option> element defines an option that can be selected. By default, the first item in
the drop-down list is selected.
Visible Values:-Use the size attribute to specify the number of visible values:
Example:
7. Text area
The <textarea> tag defines a multi-line text input control. A text area can hold an unlimited
number of characters, and the text renders in a fixed-width font (usually Courier).
The size of a text area can be specified by the cols and rows attributes.
Example:
<form>
</textarea>
9. Reset Button
Command syntax:
(i) ACTION: is the URL of the CGI (Common Gateway Interface) program that is going to
accept the data from the form, process it, and send a response back to the browser.
(ii) METHOD: GET (default) or POST specifies which HTTP method will be used to send
the form’s contents to the web server. The CGI application should be written to accept
the data from either method.
(iii)ENCTYPE: determines the mechanism used to encode the form contents. You can leave
this attribute as default (unspecified) unless you are creating a file upload field.
(iv) NAME: is a form name used by VB or JAVA scripts.
(v) TARGET: is the target frame where the response page will show up.
When the user has finished filling in the form and presses “Submit” the data is sent to the
application on the server specified in the ACTION attribute of the form element.
The application can be written in any language; however, it must be one supported by your
webserver’s operating system and webserver software.
A very popular language for creating CGI applications is PERL (Practical Extraction Report
Language).
CGI Scripts
CGI Scripts process the form's data, and send a response back to the user. They can be
written to calculate numbers as in a sales order. They can format data and put it into a
database such as a mailing list or guest book. Depending on the situation, a CGI Script can be
written to do almost anything