This document provides an overview of HTML forms and their various elements. It discusses the <form> tag and its attributes like action and method. It then describes different form elements like text fields, password fields, radio buttons, checkboxes, textareas, select boxes, and button controls. It provides examples of how to create each of these elements in HTML and explains their purpose in collecting user input for processing on the server-side.
3. Introduction
ā¢ Web forms are one of the main points of
interaction between a user and a web site or
application.
ā¢ An HTML form is used to collect user input.
ā¢ The user input is most often sent to a server for
processing.
ā¢ For example, during user registration you would
like to collect information such as name, email
address, credit card, etc.
4. Contā¦
ā¢ The back-end application (Server-side) will perform
required processing on the passed data based on
defined business logic inside the application.
ā¢ There are various form elements available like text
fields, textarea fields, drop-down menus, radio
buttons, checkboxes, etc.
ā¢ The HTML <form> tag is used to create an HTML
form and it has following syntax ā
<form action="server url" method="get|post">
//input controls e.g. textfield, textarea,
//radiobutton, button
</form>
5. <form> tag attribute
ā¢ action: Backend script ready to process your
passed data.
ā¢ method: Method to be used to upload data. The
most frequently used are GET and POST
methods.
ā¢ target: Specify the target window or frame
where the result of the script will be displayed. It
takes values like _blank, _self, _parent etc.
6. <label> Element
ā¢ The <label> element is useful for screen-reader
users, because the screen-reader will read out
loud the label when the user focus on the input
element.
ā¢ The for attribute of the <label> tag should be
equal to the id attribute of the <input> element
to bind them together.
ā¢ <input type="radio" id="html" name="fav_lang"
value="HTML">
<label for="html">HTML</label>
7. Input Element
ā¢ This is the most commonly used element within
HTML forms.
ā¢ It allows you to specify various types of user
input fields, depending on the type attribute.
ā¢ An input element can be of type text
field, password field, checkbox, radio
button, submit button, reset button, file select
box, as well as several new input
types introduced in HTML5.
ā <input type="" name="">
8. Text Fields
ā¢ Text fields are one line areas that allow the
user to input text.
ā¢ Single-line text input controls are created using
an <input> element, whose type attribute has a
value of text.
<form>
<label for="un"> Username:</label>
<input type="text" name="un" id="un">
</form>
9. Password Field
ā¢ Password fields are similar to text fields.
ā¢ The only difference is characters in a password field
are masked, i.e. they are shown as asterisks or dots.
ā¢ This is to prevent someone else from reading the
password on the screen.
ā¢ This is also a single-line text input controls created
using an <input> element whose type attribute has a
value of password.
<form>
<label for="up">Password:</label>
<input type="password" name="up" id="up">
</form>
10. Radio Buttons
ā¢ Radio buttons are used to let the user select exactly
one option from a pre-defined set of options.
ā¢ It is created using an <input> element
whose type attribute has a value of radio.
<form>
<input type = "radio" name = āGender" value = āmale">
Male
<input type = "radio" name = āGender" value = āfemale">
Female
</form>
11. Checkboxes
ā¢ Checkboxes allows the user to select one or more
option from a pre-defined set of options.
ā¢ It is created using an <input> element
whose type attribute has a value of checkbox.
ā¢ Set to checked if you want to select it by default.
<form>
<input type = "checkbox" name = āsports"> Cricket
<input type = "checkbox" name = āsports"> Tennis
<input type = "checkbox" name = āsports"> Chess
</form>
12. File Select box
ā¢ The file fields allow a user to browse for a local file
and send it as an attachment with the form data.
ā¢ This is also created using the <input> element but
type attribute is set to file.
<form>
<input type = "file" name = "fileupload" accept =
"image/*" />
</form>
ā¢ accept attribute specifies the types of files that the
server accepts.
13. Textarea
ā¢ Textarea is a multiple-line text input control that
allows a user to enter more than one line of text.
ā¢ Multi-line text input controls are created using
an <textarea> element.
<form>
Description : <br>
<textarea rows = "5" cols = "50" name = "description">
Enter description here...
</textarea>
</form>
14. Select Boxes
ā¢ A select box, also called drop down box which
provides option, from where a user can select one or
more options.
ā¢ Select box is created using the <select> element
and <option> element.
ā¢ The <option> elements within the <select> element
define each list item.
<select name = "dropdown">
<option value = "Maths" selected> Maths </option>
<option value = "Physics"> Physics </option>
</select>
15. Button Controls
ā¢ There are various ways in HTML to create
clickable buttons.
ā¢ We can create a clickable button using
<input>tag by setting its type attribute to button
/ submit / reset / image.
ā¢ type = ābuttonā creates a button that is used to
trigger a client-side script when the user clicks
that button.
ā¢ <input type="button" onclick="alert('Hello
World!')" value="Click Me!">
16. Submit & Reset Button
ā¢ A submit button is used to send the form data to
a web server.
ā¢ When submit button is clicked the form data is
sent to the file specified in the
form's action attribute to process the submitted
data.
ā¢ A reset button resets all the forms control to
default values.