Lesson 6 Forms in HTML
Lesson 6 Forms in HTML
Lesson 6
Lecturer: Dr. Geoffrey Mariga, PhD
EMAIL: gmariga@mut.ac.ke
Contents
2
6.1 About Forms
Forms are used to collect information from people viewing
your web site.
For example, you can use forms to find out details about
your visitors through surveys and feedback, or engage in
e-commerce by selling your goods and services to people.
Forms are defined by the <FORM> </FORM> tags and
are made up of different elements to collect data.
Once the user inputs all of the information, they submit the
form by using the "submit" button that you create.
What happens with the data is a decision you will need to
make.
You can use a script to manage the data, sent the data to
database, or even receive data via e-mail.
3
6.1 About Forms
4
6.2 Form Elements
All form elements should be written in between the
<form>..</form> tags.
Tag Description
<form> Defines an HTML form for user input
<input> Defines an input control
<label> Defines a label for an <input> element
<textarea> Defines a multiline input control
<select> Defines a drop-down list
<option> Defines an option in a drop-down list
<fieldset> Groups related elements in a form
<legend> Defines a caption for a <fieldset> element
<button> Defines a clickable button
5
6.2 Form Elements - <form>
The <FORM> </FORM> element is used to create an HTML form
and act as a container for form elements. Although the form element
itself isn’t usually a visible part of the page (like the body tag), it could
be with appropriate CSS.
8
6.2 Form Elements - <form>
10
6.2 Form Elements - <input>
11
6.2 Form Elements - <input>
INPUT element Attributes:
TYPE (required)
Defines the usage of the INPUT element.
Hidden inputs always have TYPE = “hidden”.
NAME provides a unique identification for INPUT element.
Each input field must have a name attribute to be submitted.
If the name attribute is omitted, the data of that input field will
not be sent at all.
VALUE indicates the value that the INPUT element sends to the
server upon submission.
SIZE attribute specifies the size for the input field. (in characters)
MAXLENGTH attribute specifies the maximum number of
characters that the input field will accept.
12
6.2 Form Elements - <input>
INPUT element Attributes:
13
6.2 Form Elements - <input>
Text Box
Text boxes allow the users to enter a single-line text.
Example
Result
14
6.2 Form Elements - <input>
Password Box
Password boxes are like text boxes, except the characters
in a password field are automatically masked. (shown as
asterisks or circles)
Example
User Name:<br>
<input type="text" name="username"><br>
Password:<br>
<input type="password" name="pswd">
Result
15
6.2 Form Elements - <input>
Radio Buttons
Usually found in a group of options, only one radio button in a group
can be selected at a time.
Selecting one radio button deselects the others in its group.
Each radio button within a group should have the same name and
different values. (Otherwise, browsers cannot distinguish between
them)
CHECKED attribute indicates which radio button is selected initially
Example
<input type="radio" name="gender" value="male"> Male<br>
<input type="radio" name="gender" value="female" checked>Female<br>
Result
16
6.2 Form Elements - <input>
Check Boxes
Check boxes let a user select NONE/ONE/MORE options of
a limited number of choices.
Each check box within a group should have the same name
and different values. (Otherwise, browsers cannot distinguish
between them)
CHECKED attribute indicates initially selected checkboxe/s.
Example
Result
17
6.2 Form Elements - <input>
Submit Button
<input type="submit"> defines a 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 (form-handler) defined in the action attribute
usually does something with the received input. (include
script for processing input data).
VALUE attribute changes the text displayed on the button
(default is “Submit”).
18
6.2 Form Elements - <input>
Submit Button
Example
Result
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".
19
6.2 Form Elements - <input>
Reset Button
A reset button is used to clear all the entries user entered into
the form and reset the form-data to its default values.
VALUE attribute changes the text displayed on the button
(default is “Reset”)
Example
<form name="input" action="html_form_action.asp" method="get">
<P>Username: <input type="text" name="user" size="25"></P>
<P>Password: <input type="password" name="pswd" size="25"></P>
<P><input type="submit" value="Submit">
<input type="reset" value="Reset"></P></form>
Result
20
7.2 Form Elements - <label>
Label
The <label> tag defines a label for an <input> element.
The <label> element does not render as anything special for
the user. However, it provides a usability improvement for
mouse users, because if the user clicks on the text within the
<label> element, it toggles the control.
The for attribute of the <label> tag should be equal to the id
attribute of the related element to bind them together.
A label can be bound to an element either by using the "for"
attribute, or by placing the element inside the <label> element.
21
7.2 Form Elements - <label>
Example
<input type="radio" name="gender" id="male" value="male"
checked>
<label for="male">Male</label><br>
22
6.2 Form Elements - <button>
The <button> element defines a clickable button.
Example
<button type="button" onclick="alert('Hello World!')">
Click Me!
</button>
Result
23
6.2 Form Elements - <textarea>
24
6.2 Form Elements - <textarea>
Example
Result
25
List Box
7.2 Form Elements - <select>,<option>
<select> tag presents a drop-down list with choices indicated by the
<option> tags
Include NAME attribute
<select name="cars">
<option selected>BMW</option>
<option>Mercedes</option>
<option>Audi</option>
</select>
Result
27
List Box
7.2 Form Elements - <select>,<option>
Example
28
7.2 Form Elements - <fieldset>,<legend>
Grouping Form Data
The <fieldset> element is used to group related data in a form.
The <legend> element defines a caption for the <fieldset> element.
Example
<fieldset><legend>Personal Information:</legend>
Name:<br>
<input type="text" name="firstname" value="your first name"><br>
Surname:<br>
<input type="text" name="lastname" value="your last name">
</fieldset>
Result
29
7.2 Form Elements
Example
<!DOCTYPE html>
<p><label><strong>Name:</strong>
<input name = "name" type = "text" size = "25">
</label></p>
<p><label><strong>Comments:</strong><br>
<textarea name = "comments" rows = "4" cols = "36"></textarea>
</label></p>
<p><label><strong>E-mail Address:</strong>
<input name = "email" type = "email" size = "25">
</label></p>
30
7.2 Form Elements
Example (cont..)
<p><strong>Things you liked:</strong><br>
<label>Site design
<input name = "thingsliked" type = "checkbox" value = "Design"></label>
<label>Links
<input name = "thingsliked" type = "checkbox" value = "Links"></label>
<label>Ease of use
<input name = "thingsliked" type = "checkbox" value = "Ease"></label>
<label>Images
<input name = "thingsliked" type = "checkbox" value = "Images"></label>
<label>Source code
<input name = "thingsliked" type = "checkbox" value = "Code"></label>
</p>
31
7.2 Form Elements
Example (cont..)
<p>
<b>Rate our site:<b>
<select name = "rating">
<option selected>10</option>
<option>9</option>
<option>8</option>
<option>7</option>
<option>6</option>
<option>5</option>
<option>4</option>
<option>3</option>
<option>2</option>
<option>1</option>
</select>
</p>
<p>
<input type = "submit" value = "Submit">
<input type = "reset" value = "Clear">
</p>
</form>
</body>
</html>
32
7.2 Form Elements
Output
33
6.3 HTML5 Input Types
HTML5 added several new input types:
color range
date search
datetime-local tel
email time
month url
number week
34
6.3 HTML5 Input Types - color
35
6.3 HTML5 Input Types - color
Example
Select your favorite color:
<input type="color" name="favcolor" value="#ff0022">
Result
36
6.3 HTML5 Input Types - date
37
6.3 HTML5 Input Types - date
Example
Birthday:
<input type="date" name="bday"><br><br>
Enter a date before 2017-11-23:<br>
<input type="date" name="bday" max="2017-11-22"><br><br>
Enter a date after 2010-12-16:<br>
<input type="date" name="bday" min="2010-12-17"><br><br>
Result
38
6.3 HTML5 Input Types - time
The purpose of the time input type is to allow the user to
enter a time.
Depending on browser support a time picker might pop-up
when you enter the input field.
Example
Select a time:
<input type="time" name="times">
Result
39
6.3 HTML5 Input Types – datetime-local
The datetime-local element combines date and time in a
single input field, with no time zone.
Depending on browser support a time picker might pop-up
when you enter the input field.
Example
Result
40
6.3 HTML5 Input Types - month
Allows the user to select a month and year.
Depending on browser support, a date picker can show up
in the input field.
Example
Result
41
6.3 HTML5 Input Types - week
Allows the user to select a week and year.
Depending on browser support, a date picker can
show up in the input field.
Example
Result
42
6.3 HTML5 Input Types – email
Used for input fields that should contain an e-mail
address.
Depending on browser support, the e-mail address can
be automatically validated when submitted.
Example
E-mail:
<input type="email" name="mail">
Result
43
6.3 HTML5 Input Types - number
Defines a numeric input field.
You can also set restrictions on what numbers are
accepted by using minand max attributes.
Example
Result
44
6.3 HTML5 Input Types - range
Defines a control for entering a number whose exact
value is not important.
Default range is 0 to 100. However, you can set
restrictions on what numbers are accepted with the
min and max attributes.
Depending on browser support, the input type "range"
can be displayed as a slider control.
Example
Grade:
<input type="range" name="points" min="0" max="100">
Result
45
6.3 HTML5 Input Types - tel
Example
Telephone:
<input type="tel" name="telephone">
Result
46
6.3 HTML5 Input Types - search
Used for search fields.
A search field behaves like a regular text field.
Example
Search Google:
<input type="search" name="googlesearch">
<input type="submit" value="Search">
Result
47
6.3 HTML5 Input Types - url
Used for input fields that should contain a URL
address.
Depending on browser support, the url field can be
automatically validated when submitted.
Example
Add your homepage:
<input type="url" name="homepage">
<input type="submit" value="Submit">
Result
48