Professional Documents
Culture Documents
COMPUTER 8 MODULE 13 and 14
COMPUTER 8 MODULE 13 and 14
8
Fundamentals of Web
Application Development
Module 13 - 14 (Week 1, 2, and 3)
Creating a Form
Creating Form Elements
Give an example of HTML Form tag elements. Define what the purpose of it.
This chapter will show you how to create forms and use Javascript to process the data in
it. It will explain how form elements such as textboxes, checkboxes, radio buttons, and drop-down
menus offer a better way to accept data from users compared to prompt() function.
Page | 2
USING FORMS
HTML forms allow you to get data from users in a more sophisticated manner. Compared
to the prompt () function, they provide you with objects such as textboxes, buttons, checkboxes,
and drop-down boxes. They give you better control over the users’ inputs by selecting the
appropriate object that you will use to gather specific pieces of data. Also, forms are more
presentable and organized for the user to manipulate. A form is created using HTML tags.
Javascript may be used to retrieve and manipulate the values of elements in the form.
In the next sections, you will learn how to create a form using HTML. Afterward, you will
create Javascript functions that will handle the values that were entered by the users on the form.
Exercises will help you apply what have you learned.
Creating a form will require the use of HTML. The next section will show you how.
A. Creating a Form
A form is defined using the <form> and </form> tags.
Syntax:
<form method=”get/post” id=”formid” name=”formname” action=”URL”>
</form>
Where:
method – this is optional. The method on how to send the data to another file. The default
is get. Use post if the form content exceeds 100 characters.
id – this is optional. This the unique id of the form object. The object id will be used to
retrieve the values in the form using the documents.getElementById() function.
Page | 3
name – it is unique name of the form object. The object name can also be used to retrieve
the values in the form.
action – This is optional. The url of the file that directs where to send the form values.
Example:
<form method=”get” name=”sampleform” action=”process.asp”>
</form>
The example above creates a form, having sampleform as the id. The values in the form will be
sent to process.asp. Do not worry about process.asp, you will learn more about it in the next
chapters. Let us just focus on creating forms.
Syntax:
Where:
type=“text” – it creates a textbox.
type=“password” – it creates a textbox with masking.
size – this is optional. It defines the width of the textbox by specifying the number
of characters.
maxlength – this is optional. This indicates the number of characters the textbox
will accept.
id – this is optional. This is unique id of the object. The object id will be used to
retrieve the values in the object using the document.getElementById() function.
name – this is name of the object. The name is also used to retrieve values in the
object.
value – this is initial value of the textbox
Example:
<html>
<head>
<title>Sample Form</title>
</head>
<body>
<form name=”sample form”>
First Name: <input type=”text” name=”firstname” value=””/>
<br/><br/>
Last Name: <input type=”text” name=”lastname” value=”” />
</form>
</body>
</html>
Page | 4
Result:
<html>
<head>
<title>Sample Form</title>
</head>
<body>
<form name=”sample form”>
Password: <input type=”password” name=”magicword”
value=””/>
</form>
</body>
</html>
Result:
b. A textarea is used to allow the user to enter data in multiple lines. A textarea is also
called a multi-line textbox. A textarea is defined using the <textarea> and </textarea>
tags.
Syntax:
<textarea cols=”n” rows=”n” id=”objectid”
name=”name”>text</textarea>
Where:
Page | 5
name - the name of the object. The name is also used to retrieve the values in
the object.
text – this is optional. The initial content of the textarea.
Example:
<html>
<head>
<title>Sample Form</title>
</head>
<body>
<form name=”sampleform”>
Please say something about yourself.
<textarea rows=”10” cols=”20” name=”additionalinfo”>
</textarea>
</form>
</body>
</html>
Result:
c. A button or command button is the main clickable element in a form. A button is defined
using the <input> tag.
Syntax:
<input type=”button/reset/submit” name=”name”
value=”initialvalue”>
Where:
Page | 6
value – this is the caption of the button.
Example:
<html>
<head>
<title>Sample Form</title>
</head>
<body>
<form name=”sampleform” action=”process.asp”>
First Name: <input type=”text” name=”firstname” value=”” />
<br/><br/>
Last Name: <input type=”text” name=”lastname” value=”” />
<br/><br/>
<input type=”reset” value=”Reset this form” />
<input type=”submit” value=”Submit this form” />
</form>
</body>
</html>
Result:
d. Radio buttons or option buttons are used when you want the user to select from a set
of choices. A radio button is defined using the <input> tag.
Syntax:
<input type=”radio” id=”objectedId” name=”name”
value=”initialvalue” />
Where:
Example:
<html>
<head>
<title>Sample Form</title>
Page | 7
</head>
<body>
<form name=”sampleform”>
Gender: <input type=”radio” name=”gender” value=”male” />
<input type=”radio” name=”gender” value=”female” />
</form>
</body>
</html>
Result:
e. checkboxes are used to allow the users to select more than one from a given set of
choices. A check box is defined using the <input> tag.
Syntax:
<input type=”checkbox” id=”objectedId” name=”name”
value=”initialValue”>
Where:
Example:
<html>
<head>
<title>Sample Form</title>
</head>
<body>
<form name=”sampleform”>
How do you go to school? <br/>
<input type=”checkbox” name=”transportation1”
value=”car” /> Car
<input type=”checkbox” name=”transportation2”
value=”bus” /> School Bus
<input type=”checkbox” name=”transportation3”
value=”commute” /> Commute
</form>
</body>
</html>
Page | 8
Result:
Where:
Page | 9
Result:
g. A hidden form element is used when you want an object to store a value as part of the
form without it being displayed. A hidden element is defined using the <input> tag.
Syntax:
<input type=”hidden” id=”objectid” name=”name” value=”value”>
Where:
Example:
<html>
<head>
<title>Sample Form</title>
</head>
<body>
<form name=”sampleform”>
Please say something about yourself. <br>
<textarea row=”10” cols=”40” name=”additionalinfo”>
</textarea>
<input type=”hidden” name=”recordnumber” value=”1”>
</form>
</body>
</html>
Page | 10
Result:
There are actually two form elements on the page. The first one is the textarea,
and the second is the hidden element named recordnumber that has a value of 1. The
value of the hidden element may be used later by another script.
Page | 11
COMPUTER SCIENCE 8 (MODULE 13 and 14)
Identify the following questions. Write your answer in the space provided.
1. It allow the users to select more than one from a given set of choices.
2. It is used when you want an object to store a value as part of the form
without it being displayed.
Page | 12
Give an example of a checkboxes form. Write the code and draw its output. (Use the back of
this page if necessary).
Exercise: Write a code that displays a similar output to the given picture.
(An additional points to those who can submit a code).
Page | 13