Topic 3 CSS
Topic 3 CSS
Uses of Form
Forms are used in following manner
(1) Forms are used to collect the information from customer or students for online registrations
(2) Forms are used for online survey.
(3) Forms are used for conducting online examination, for getting feedbacks and so on.
(4) The information present in the form is submitted to the server for further processing
Attribute Description
target It specifies the target of the address in the action attribute The target values can be as follows -
TM
Client Side Scripting Language 3-2 Form and Event Handling
<body>
<form name=”myform” action=”/myServerPage.phtp” method=”GET” target=”_blank”>
//code for placing form controls here
…
…
…
</form>
</body>
3.1.3 Text
Text is typically required to place one line text. For example if you want to enter some name then it is
always preferred to have Text field on the form.
The text field can be set using
<input type="text" size=”30” name =”username” value=" ">
The input type is text and the value of this text field is “ ” That means the blank text field is displayed
initially and we can enter the text of our choice into it. There is size parameter which allows us to
enter some size of the text field.
Some other parameters or attributes can be
o maxlength that allows us to enter the text of some maximum length.
o name indicates name of the text field.
o align denotes the alignment of the text in the text field. The alignment can be left, right, bottom
and top.
Example Code
Output
Script Explanation :
In above document
1) We have the label “Input String” just before the <input> tag. We can also specify the label by using
the <label> tag as follows -
<label>Input String: <br/><input type="text" size="25" value=""></label>
2) Thus the label gets bound to the text box. This aspect is always beneficial for a web programmer
because using label control we can focus on the corresponding text box contents.
3) Initially the text box field is blank. We can type some text inside this text box.
Ex. 3.1.1 : How will you create password field in a HTML form ?
Sol. : The password field is typically created on the form. Hence following code can be written to create it -
<form name=”form1”>
Password:<input type=”password”/>
</form>
</body>
</html>
Output
Various parameters that can be set for the text area can be
row denotes total number of rows in the text area.
col specifies total number of columns in the text area.
name denotes the name of the text area which can be utilised for handling that component for some
specific purpose.
wrap can be virtual or physical. If the wrap is virtual then the line breaks get disappeared when the
text is actually submitted to the server. But if the wrap is assigned to the physical then the line breaks
(if any) appear as it is in the text.
3.1.5 Checkbox
It is the simplest component which is used particularly when we want to make some selection from
several options.
For having the checkbox we have to specify the input type as checkbox. For example
<input type="checkbox" name="option1" value="mango" checked="checked">Mango<br/>
If we want to get the checkbox displayed as checked then set checked="checked"
Example Code
HTML Document[ChkBoxDemo.html]
<!DOCTYPE html>
<html>
<head>
<title>My Form with Check box</title>
</head>
<body>
<form name ="checkboxForm">
<div align="center"><br>
Select the fruit(s) of your choice<br/>
Client Side Scripting Language 3-5 Form and Event Handling
Script Explanation
1) In the above program to set some checkbox in checked state we can mention the attribute checked as
checked.
2) We can set the value attribute as “ “ but this then the checkbox will not get associated with any value.
The Mango, Apple and Guava are the labels of the checkboxes.
HTML Document[RadioButDemo.html]
<!DOCTYPE html>
<html >
<head>
<title>My Form with radio buttons Page</title>
</head>
<body>
<form name="myform">
<div align="left"><br>
<b>Select Fruit which you like the most</b><br/>
Client Side Scripting Language 3-6 Form and Event Handling
Ex. 3.1.2 : What is the difference between group of checkbox buttons and group of radio buttons ?
Sol. : ● The checkbox and radio buttons are used for making the selection from a group of choices.
When a user selects (checks) a checkbox, its value gets assigned as the current value of the checkbox
group's control name.
A checkbox group's control name may get paired with several current values if the user selects more
than one checkbox.
Radio buttons work just like checkboxes except they are typically set up to be mutually exclusive of
one another, i.e. when one is selected, all the others are automatically deselected.
3.1.7 Button
We can create the button using <input type =”button”> tag.
There are two types of buttons that can be created in HTML. One is called submit button and the
another one is reset button.
Various parameters of submit button are
1) name denotes the name of the submit button.
2) value is for writing some text on the text on the button.
3) align specifies alignment of the button.
Client Side Scripting Language 3-7 Form and Event Handling
Example Code
HTML Document[Button.html]
<!DOCTYPE html>
<html >
<head>
<title> My Page </title>
</head>
<body>
<form name="myform" action="http://www.localhost.com/cgi-bin/hello.cgi" method="POST">
<div align="center">
<br/><br/>
<input type="text" size="35" value=" ">
<br><input type="submit" value="Send">
<input type="reset" value="Reset"><br>
</div>
</form>
</body>
</html>
Output
Script Explanation
1) In above HTML document, we have used the form whose name is “myform”.
2) There are two attributes associated with the form tag and those are action and method. The action
parameter indicates the address and the cgi script where the contents should go and method
parameter is for the methods for submitting the data. The method can be get or post. Thus by
specifying the action and method for a form we can send the desired data at desired location.
Client Side Scripting Language 3-8 Form and Event Handling
Ex. 3.1.3 : Create a HTML document that has the form with the following controls
a) A text box to collect the customer name.
b) Four checkboxes, one for the following items :
i. Four HTML textbooks for ` 1000 ii. Eight XML textbooks for ` 2000
iii. Four Javabeans books for ` 2500 iv. Eight UML textbooks for ` 1500
c) A collection of radio buttons that are labelled as follows –
i. Cash ii. Cheque/DD iii. Creadit card.
Sol. : <!DOCTYPE html>
<html >
<body>
<center><h2>REGISTRATION FORM</h2></center>
<hr/>
<form>
<table>
<tr>
<td>Name:</td>
<td><input type="text" size="25"value=""></td>
</tr>
<tr>
<td>Select the desired Items:</td>
<td>
<select name="f">
<option value="4 HTML Books(Rs. 1000)">4 HTML Books(Rs. 1000)</option>
<option value="8 XML Books(Rs. 2000)">8 XML Books(Rs. 2000)</option>
<option value="4 JavaBeans Books(Rs.2500)">4 JavaBeans Books(Rs.2500)</option>
<option value="8 UML Books(Rs.1500)">8 UML Books(Rs.1500)</option>
</select>
</td>
</tr>
<tr>
<td>
Mode of Payment:
</td>
</tr>
<tr>
<td><input type="radio" name="group1" value="Cash">Cash</td>
</tr>
<tr>
<td><input type="radio" name="group1" value="Cheque/DD">Cheque/DD</td>
</tr>
<tr>
Client Side Scripting Language 3 - 10 Form and Event Handling
Output
Ex. 3.1.5 : Design a simple HTML form for filling the information for registration of a student.
Sol. : HTML Form for Student Registration
<!DOCTYPE html>
<html >
<head>
<title>My Page</title>
</head>
<body>
<form>
<b>Student Name:</b><input type="text" size="20" value=""><br/><br/>
<b>Address:</b><input type="text" size="35" value=""><br/><br/>
<b>Email:</b><input type="text" size="20" value=""><br/><br/>
<b>Password:</b><input type="password" size="10" value=""><br/><br/>
<b>Select Course:</b><br/><br/>
<input type="radio" name="courses" value="Computer Engineering">Computer Engineering<br/><br/>
<input type="radio" name="courses" value="E&TC Engineering">E&TC Engineering<br/><br/>
<input type="radio" name="courses" value="Mechanical Engineering">Mechanical
Engineering<br/><br/>
<input type="radio" name="courses" value="Civil Engineering">Civil Engineering<br/><br/>
<b>Select Payment Mode:</b>
Client Side Scripting Language 3 - 12 Form and Event Handling
<select name="MyMenu">
<option value="Cheque">Cheque</option>
<option value="Cash">Cash</option>
<option value="Card">Card</option>
</select>
<br/><br/><br/>
<input type="submit" value="Submit"/>
<input type="reset" value="Clear"/>
</form>
</body>
</html>
Output
Commonly used events and tag attributes are enlisted in the following table -
keyup onkeyup When user releases the key from the Form elements such as
keyboard. input,button,text,textarea and so on.
keydown onkeydown When user presses the key down Form elements such as
input,button,text,textarea and so on.
keypress onkeypress When user presses the key. Form elements such as
input,button,text,textarea and so on.
mousedown onmousedown When user clicks the left mouse button. Form elements such as
input,button,text,textarea and so on.
mouseup onmouseup When user releases the left mouse Form elements such as
button. input,button,text,textarea and so on.
mousemove onmousemove When user moves the mouse. Form elements such as
input,button,text,textarea and so on.
mouseout onmouseout When the user moves the mouse away Form elements such as
from some element input,button,text,textarea and so on.
mouseover onmouseover When the user moves the mouse away Form elements such as
over some element input,button,text,textarea and so on.
Table 3.2.1
JavaScript[OnloadDemo.html]
<!DOCTYPE html>
<html >
<head>
<title>Demo of onload Tag Attibute</title>
<script type="text/javascript">
function my_fun()
{
//This message will be displayed on page loading
<body onload="my_fun()">
</body>
</html>
Output
Client Side Scripting Language 3 - 16 Form and Event Handling
Event Description
For example -
Following is a simple JavaScript in which on the button click we have called a function my_fun(). This
is a simple function in which we have displayed some message using alert popup box.
JavaScript[OnclickDemo.html]
<!DOCTYPE html >
<html >
<head>
<title>Demo of onlclick Tag Attibute</title>
<script type="text/javascript">
function my_fun()
{
alert("Hello I am in my function");
}
</script>
</head>
<body>
<center>
<form>
<input type="button" value="Click" onclick="my_fun()">
</form>
</center>
</body>
</html>
Client Side Scripting Language 3 - 17 Form and Event Handling
Output
Ex. 3.2.1 : Insert an image into a web page. Write a script which displays the message when the mouse is over the
image. The co-ordinates of the mouse should be displayed if click attempted on the image.
Sol. : <html>
<head>
<script type="text/javascript">
function my_fun1()
{
points.innerText="Now the mouse is moving";
}
function my_fun2()
{
points.innerText="("+event.offsetX+" , "+event.offsetY+")";
}
</script>
</head>
<body onmousemove="my_fun1()" onmousedown="my_fun2()">
<center>
<span id="points">(0,0)</span>
<img src="d:\flower1.gif" style="position:absolute;top:50;left:90">
</center>
</body>
</html>
Client Side Scripting Language 3 - 18 Form and Event Handling
Output
Event Description
onkeydown When user presses the key on the keyboard.(this happens first)
onkeyup The user releases a key that was down.(this happens last).
JavaScript Example
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function MyKeyfunction()
{
alert("You have pressed the key");
}
</script>
</head>
<body>
<form>
Enter Name: <input type="text" onkeypress="MyKeyfunction()"/>
</form>
</body>
</html>
Client Side Scripting Language 3 - 19 Form and Event Handling
Output
<script type="text/javascript">
function Myfunction()
{
alert("You have entered the name: "
+document.forms.myform.username.value);
}
</script>
</head>
<body>
<form name="myform">
Enter Name: <input type="text" name="username"/>
<input type="button" value="Click Me" onclick="Myfunction()"/>
</form>
</body>
</html>
Output
(5) Using the alert popup box the text typed within the textbox will be displayed.
We can modify the above given Myfunction as follows for efficient use
function Myfunction()
{
with(document.forms.myform.username)
{
alert("You have entered the name: "+value);
}
}
JavaScript Example
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function ChangeMe(Ele)
{
Ele.style.background='pink'
}
</script>
</head>
<body>
<form name="myform">
Enter RollNo: <input type="text" name="roll" onchange="ChangeMe(this)"/>
<br/><br/>
Enter Name : <input type="text" name="name" onchange="ChangeMe(this)"/>
<br/>
<input type="submit" value="Submit"/>
</form>
</body>
</html>
Client Side Scripting Language 3 - 22 Form and Event Handling
Output
choices[0].text="Rose"
choices[0].value=1
choices[1].text="Jasmine"
choices[1].value=2
choices[2].text="Lotus"
choices[2].value=3
}
if(val==3)
{
choices[0].text="Red"
choices[0].value=1
choices[1].text="Green"
choices[1].value=2
choices[2].text="Blue"
choices[2].value=3
}
}
}
</script>
</head>
<body>
<h4>Select the Desired Category</h4>
<form name="myform">
<input type="radio" name="items" checked="true" value=1
onclick=MySelection(this.value)>Fruit
<input type="radio" name="items" value=2
onclick=MySelection(this.value)>Flower
<input type="radio" name="items" value=3
onclick=MySelection(this.value)>Colour
<br/><br/>
<select name="choices">
<option Value=1>Mango
<option Value=2>Orange
<option Value=3>Banana
</select>
</form>
</body>
</html>
Client Side Scripting Language 3 - 24 Form and Event Handling
Output
JavaScript Document
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function MyFunction()
{
var str="You have selected: ";
Client Side Scripting Language 3 - 25 Form and Event Handling
with(document.forms.myform)
{
if(red.checked==true)
str+=" Red";
if(blue.checked==true)
str+=", Blue";
if(green.checked==true)
str+=", Green";
if(yellow.checked==true)
str+=", Yellow";
}
alert(str);
}
</script>
</head>
<body>
<h3> Select color(s) of your choice: </h3>
<form name="myform">
<input type="checkbox" name="red" value="red"> Red<br/>
<input type="checkbox" name="blue" value="blue"> Blue<br/>
<input type="checkbox" name="green" value="green"> Green<br/>
<input type="checkbox" name="yellow" value="yellow"> Yellow<br/>
<input type="button" name="Color" value="Choose Color" onclick="MyFunction()">
</form>
</body>
Output
Client Side Scripting Language 3 - 26 Form and Event Handling
JavaScript Document
<html>
<head>
<script type="text/javascript">
function MySelection(val)
{
with(document.forms.myform)
{
if(val=="Fruit")
{
Button_Label.value="Flower"
choices[0].text="Rose"
choices[0].value=1
choices[1].text="Jasmine"
choices[1].value=2
choices[2].text="Lotus"
choices[2].value=3
}
if(val=="Flower")
{
Button_Label.value="Fruit"
choices[0].text="Mango"
choices[0].value=1
choices[1].text="Orange"
choices[1].value=2
choices[2].text="Banana"
choices[2].value=3
}
}
}
</script>
</head>
<body>
<h4>Select the Desired Category</h4>
<form name="myform">
<select name="choices">
<option Value=1>Mango
<option Value=2>Orange
<option Value=3>Banana
</select>
Client Side Scripting Language 3 - 27 Form and Event Handling
JavaScript Document
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function MyFunction()
{
with(document.forms.myform)
{
if(name.value.length>0 && roll.value.length>0)
regid.value=name.value.charAt(0)+
name.value.charAt(1)+
roll.value
Client Side Scripting Language 3 - 28 Form and Event Handling
}
}
</script>
</head>
<body>
<form name="myform">
Roll Number:<input type="text" name="roll"/>
<br/><br/>
Name :<input type="text" name="name"/>
<br/><br/>
Reg.ID :<input type="hidden" name="regid"/>
<br/><br/>
<input type="submit" name="Submit" value="Submit" onclick="MyFunction()"/>
</form>
</body>
</html>
JavaScript Example
<!DOCTYPE html>
<html>
<body>
<form name="myform">
Roll Number:<input type="text" name="roll"/>
<br/><br/>
Name :<input type="text" name="name"/>
<br/><br/>
<img src="submit.gif" onclick="javascript:document.forms.myform.submit()"/>
<br/><br/>
</form>
</body>
</html>
For example
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function EnableFunction()
{
document.forms.myform.name.disabled=false
}
function DisableFunction()
{
document.forms.myform.name.disabled=true
}
</script>
</head>
<body>
<form name="myform">
User Name:<input type="text" name="name"/>
<br/><br/>
<input type="button" value="Disable Name Field" onclick="DisableFunction()"/>
<br/><br/>
<input type="button" value="Enable Name Field" onclick="EnableFunction()"/>
</form>
</body>
</html>
Output
JavaScript Example
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function ReadOnlyFunction()
{
document.forms.myform.name.readOnly=true
}
</script>
</head>
<body>
<form name="myform">
User Name:<input type="text" name="name"/>
<br/><br/>
<input type="button" value="ReadOnly Name Field" onclick="ReadOnlyFunction()"/>
</form>
</body>
</html>
Output