INSY214 Chapter6
INSY214 Chapter6
CHAPTER 6
LOGO
Contents
2
D. Gichuki
7.1 About Forms
4
D. Gichuki
7.2 Form Elements
8
D. Gichuki
7.2 Form Elements - <form>
10
D. Gichuki
7.2 Form Elements - <input>
11
D. Gichuki
7.2 Form Elements - <input>
13
D. Gichuki
7.2 Form Elements - <input>
Text Box
Text boxes allow the users to enter a single-line text.
Default width of a text field is 20 characters.
Example
Result
14
D. Gichuki
7.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
D. Gichuki
7.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
D. Gichuki
7.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
<input type="checkbox" name="choice" value="cb1" checked>Love
<br>
<input type="checkbox" name="choice" value="cb2">Cash <br>
<input type="checkbox" name="choice" value="cb3"
checked>Education <br>
Result
17
D. Gichuki
7.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
D. Gichuki
7.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
D. Gichuki
7.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
D. Gichuki
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
D. Gichuki
7.2 Form Elements - <label>
Label
Example
<input type="radio" name="gender" id="male" value="male"
checked>
<label for="male">Male</label><br>
22
D. Gichuki
7.2 Form Elements - <button>
Button
The <button> element defines a clickable button.
Example
<button type="button" onclick="alert('Hello World!')">
Click Me!
</button>
Result
23
D. Gichuki
7.2 Form Elements - <textarea>
Text Area
Inserts a scrollable text box into FORM for entering
multi-line text.
It is commonly used in situations where you ask for info
that may require multiple sentences.
You control the dimension of the text area by using the
ROWS and COLS attributes.
The rows attribute specifies the visible number of lines
in a text area.
The cols attribute specifies the visible width of a text
area.
24
D. Gichuki
7.2 Form Elements - <textarea>
Text Area
Example
Result
25
D. Gichuki
7.2 Form Elements - <select>,<option>
List Box
<select> tag presents a drop-down list with choices
indicated by the <option> tags
Include NAME attribute
Example
<select name="cars">
<option selected>BMW</option>
<option>Mercedes</option>
<option>Audi</option>
</select>
Result
27
D. Gichuki
7.2 Form Elements - <select>,<option>
List Box
Example
28
D. Gichuki
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
D. Gichuki
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
D. Gichuki
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
D. Gichuki
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
D. Gichuki
7.2 Form Elements
Output
33
D. Gichuki
7.3 HTML5 Input Types
color range
date search
datetime-local tel
email time
month url
number week
34
D. Gichuki
7.3 HTML5 Input Types - color
35
D. Gichuki
7.3 HTML5 Input Types - color
Example
Select your favorite color:
<input type="color" name="favcolor" value="#ff0022">
Result
36
D. Gichuki
7.3 HTML5 Input Types - date
Setting the input type to date indicates that you wish the
user to enter a date.
Depending on browser support, a date picker can show up
in the input field.
You can restrict the dates allowed to a specific range by
applying the min and max attributes to the element.
37
D. Gichuki
7.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
D. Gichuki
7.3 HTML5 Input Types - time
Example
Select a time:
<input type="time" name="times">
Result
39
D. Gichuki
7.3 HTML5 Input Types – datetime-local
Example
Result
40
D. Gichuki
7.3 HTML5 Input Types - month
Result
41
D. Gichuki
7.3 HTML5 Input Types - week
Example
Result
42
D. Gichuki
7.3 HTML5 Input Types – email
Example
E-mail:
<input type="email" name="mail">
Result
43
D. Gichuki
7.3 HTML5 Input Types - number
Example
44
D. Gichuki
7.3 HTML5 Input Types - range
Example
Grade:
<input type="range" name="points" min="0" max="100">
Result
45
D. Gichuki
7.3 HTML5 Input Types - tel
Example
Telephone:
<input type="tel" name="telephone">
Result
46
D. Gichuki
7.3 HTML5 Input Types - search
Example
Search Google:
<input type="search" name="googlesearch">
<input type="submit" value="Search">
Result
47
D. Gichuki
7.3 HTML5 Input Types - url
Example
Add your homepage:
<input type="url" name="homepage">
<input type="submit" value="Submit">
Result
48
D. Gichuki