Lesson 6 Modified
Lesson 6 Modified
Lesson Introduction
In the previous lesson you have obtained skills to structure your web pages with structuring
elements such as tables, list etc. For a website design to be successful, you must consider much
more than initial functionality where as your visitors and users experience of your site. There
are many aspects of your website that contribute to the overall user experience. A web form is
one of the best ways to get input from prospective clients and indirectly establish a cordial
relation with them. During this week students will create web forms for the user interaction.
Here the forms will be created with interactive web elements for data feeding and interaction
through mouse and keyboard actions. Further, the necessary attributes for data processing to
be taken place will also be discussed.
Learning Outcomes:
After completion of this lesson, the learner will be able to create web forms according to the
given parameters and formats with basic form elements.
Use form element with its attributes
Set form size, color and other formatting
Apply form actions with respect to the requirements
Insert buttons and text boxes into the form
Add the submit and re set buttons
Add Drop down boxes with data values
Insert radio buttons with values and groups
Insert check boxes with relevant groups
Add text areas with scroll options
Create Iframe elements effectively
Lesson outline
Form tag and its attributes
Input elements inside a form and their attributes
Basic user interaction buttons and their events
Drop down boxes and data values
Radio buttons and groups
Check boxes and groups
Text areas and scrolls
Iframes and web page within another web page
6.1 Form tag and attributes
HTML Forms are one of the main points of interaction between a user and a web site or
application. They allow users to send data to the web system. Most cases that data is sent to
the back end web server for processing. An HTML form is a section of a document containing
normal content, markup, special elements called as controls (checkboxes, radio buttons,
menus, etc.), and labels on those controls. Users generally fill a form by modifying its controls
(entering text, selecting menu items, etc.), before submitting the form data for processing (e.g.,
to a Web server, to a mail server, etc.)
The HTML <form> tag is used to create a form for user inputs. The <form> tag indicates the
browser where the form starts and ends. You can add all kinds of HTML tags between the
<form> and </form> tag. For example, a form can easily include a table or an image along with
the form fields. The following code demonstrate the syntax of <form> element.
<html>
<head>
<title>My First Form</title>
</head>
<body>
<! -- Here goes HTML -->
<form>
<! -- Here goes form controls and HTML -->
</form>
<! -- Here goes HTML -->
</body>
</html>
<form> tag supports some specific attributes to configure the way the form to be reacted. Most
of its attributes are optional but it's considered best practice to always set at least the action
attribute and the method attribute.
There are quite a few different form elements you can add to a form. Lets discuss one by one.
<input> Element:
The most important form element is the <input> element. It is used to create interactive
controls for web‐based forms to accept data from the user. The <input> element can be
displayed in several ways, depending on the type attribute.
In each case, the initial value (text) produces a basic text input, but you can try other values
such as checkbox, radio, submit, reset, button, password or hidden.
In HTML button represents a clickable button, which can be used in forms, or anywhere in a
document that needs simple, standard button functionality. By default, HTML buttons are
typically presented in a style similar to that of the host platform the user agent is running on,
but you can change the appearance of the button using CSS.
There are three basic buttons in the form element. They are button, submit and reset. Submit
is the most important button that is used to send the information in a form to a server. Reset
button will clear the content of the form or set in to default values. Normal button can be use
to add any clickable event to the form.
As you can see here if you want to add your own text to the submit button you have to define
the value attribute. Thts is way this submit button appers as “SEND”. If not it will take the
default value as “submit”.
Following table shows the summary of the <input> element that we discussed so far.
Attribute Value Description
text A single‐line text field. Line‐breaks are automatically removed
from the input value
password A single‐line text field whose value is hidden. Use the maxlength
attribute to specify the maximum length of the value that can be
entered.
hidden A control that is not displayed but whose value is submitted to
type
the server. This specifically can't be edited or seen by the user
on the user interface, although you could edit the value via
browser developer tools.
submit A button that submits the form to the server.
reset A button that resets the contents of the form to default values
button A push button with no default behavior (action)
name any name The name of the control, which is submitted with the form data
size any number The size attribute specifies the visible width, in characters.
any name The initial value of the control. This attribute is optional except
value
when the value of the type attribute is radio or checkbox.
Let’s create a simple form by including all the input types that mentioned in the above table.
Try to understand to usage of each input type in the following form.
<html>
<head>
<title>My Page</title>
</head>
<body>
</form>
</body>
</html>
6.4 Radio buttons and groups
Radio buttons are used when you want to let the visitor select only one option from a set of
alternatives. It is a <input> elements type that generally used in radio groups or collections of
radio buttons describing a set of related options. If more options are to be allowed at the same
time you should use check boxes instead. Radio buttons are typically rendered as small circles,
which are filled or highlighted when selected.
group one
<form>
<p>Please select your preferred contact method:</p>
group two
<p>Please select your Gender:</p>
<input type="submit">
</form>
For example, if your form needs to ask the user for their preferred contact method, and your
gender, you might create three radio buttons for preferred contact, each with the name
property set to "contact" but one with the value "email", one with the value "phone", and one
with the value "mail". For gender, each with the name property set to "gender”. The user never
sees the value or the name (unless you expressly add code to display it).
When the above form is submitted with a radio buttons selected, the form's data includes an
entry in the form "contact=value". For example, if the user clicks on the "Email" radio button
then submits the form, the form's data will include the line "contact=email".
<input> elements of type checkbox are rendered by default as square boxes that are checked
(ticked) when activated, like you might see in many paper forms.
Checkboxes are similar to radio buttons, but with an important distinction: radio buttons are
designed for selecting one value out of a set, whereas checkboxes let you turn individual values
on and off. Where multiple controls exist, radio buttons allow one to be selected out of them all
and checkboxes allow multiple values to be selected.
The example available above only contained one checkbox; in real‐world situations you'll be
likely to encounter multiple checkboxes. If they are completely unrelated, then you can just
deal with them all separately, as shown above.
For example, in the following code example you can try out to include multiple checkboxes to
allow the user to select their interests (as many as they prefer).
The following code only contains the <form> code snippet; therefore, you have to fill the other
necessary HTML elements when you create it as a web page. Try this code with your
modifications.
<form>
</form>
In the above example, you saw that we've given each checkbox the same name. If both
checkboxes are checked and then the form is submitted, you'll get a string of name/value pairs
submitted like this: interest=coding&interest=sports. When this data reaches the server‐side,
you should be able to capture it as an array of related values and deal with it appropriately.
The <select> element is used to create a drop‐down list. The <option> tags inside the <select>
element define the available options in the list. By default, the first item in the drop‐down list is
selected. To define a pre‐selected option, add the selected attribute to the option.
Try the following code snippet by adding necessary HTML elements as it contains only the
select element portion.
<form>
<p>Select your favorite sport:</p>
As shown in the above example the size attribute changes the looks of the control considerably
by setting the number of lines (no of visible options), the control display at a time. Do the above
example by adding the necessary HTML codes and create a page to observe the output.
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.
However, when you need a field that can accept any combination and length of plain text
letters, numbers, and symbols, <textarea> is the element you are looking for.
Above example snippet shows the usage of <textarea> element. Try the above textarea
example by filling the missing HTML components to observe the output.
6.8 Iframes and web page within another web page
The <iframe> creates an inline frame, which embeds an independent HTML document into the
current document. The src attribute is used to specify the URL of the document that occupies
the inline frame.
<iframe src="URL"></iframe>
You can examine more examples by clicking here. Perform the examples given in this links for
your skill enhancement.
An iframe can be used as the target frame for a link. Which means when you click on the given
link, the target page of that link will be displayed in the iframe. The target attribute of the link
must refer to the name attribute of the iframe.
To get a better understand about the iframe and the target link watch this video.
<p>When the target of a link matches the name of an iframe, the link
will open in the iframe.</p>
</body>
</html>
Perform the above code on your machine and observe the output. Try to do some
modifications to the code where you can get more experience.
Activity 6.1