HTML FORMS
INTRODUCTION
HTML Forms are required when you want to collect some
data from the site visitor.
For example, during user registration you would like to
collect information such as name, email address, etc.
A form will take input from the site visitor and then will post
it to a back-end application such as JSP
The back-end application will perform required processing
on the passed data based on defined business logic inside the
application.
FORM INPUT ELEMENT
The form input element is used to collect details from the user.
FORM SYNTAX
<form action=“URL” method=“GET | POST>
form elements
</form>
FORM ATTRIBUTES
action="url"
Specifies where to send the data when the Submit button is clicked
method="get" (default)
Form data is sent as a URL with ?form_data info appended to the end
Can be used only if data is all ASCII and not more than 100 characters
method="post"
Form data is sent in the body of the URL request
Cannot be bookmarked by most browsers
target="target“
Tells where to open the page sent as a result of the request
target= _blank means open in a new window
target= _top means use the same window
FORM ELEMENTS
• There are different types of form controls that
you can use to collect data using HTML form:
Text Input Controls
Checkboxes Controls
Radio Box Controls
Select Box Controls
File Select boxes
Hidden Controls
Clickable Buttons
Submit and Reset Button
TEXT INPUT CONTROLS
• There are three types of text input used on forms:
– Single-line text input controls - This control is used for
items that require only one line of user input, such as
search boxes or names. They are created using HTML
<input> tag.
– Password input controls - This is also a single-line text input
but it masks the character as soon as a user enters it. They
are also created using HTML <input> tag.
– Multi-line text input controls - This is used when the user is
required to give details that may be longer than a single
sentence. Multi-line input controls are created using HTML
<textarea> tag.
SINGLE LINE INPUT CONTROLS
• This control is used for items that require only one line of user input, such
as search boxes or names. They are created using HTML <input> tag.
<html>
<head>
<title>Text Input Control</title>
</head>
<body>
<form >
First name: <input type="text" name="firstname" />
<br>
Last name: <input type="text" name="lastname" />
</form>
</body>
</html>
ATTRIBUTES FOR TEXT CONTROL
PASSWORD INPUT CONTROLS
• This is also a single-line text input but it
masks the character as soon as a user enters
it.
• They are also created using HTML <input>
tag but type
attribute is set to password.
<html>
<head>
<title>Password Input Control</title>
</head>
<body>
<form >
User ID : <input type="text" name="user_id" />
<br>
Password: <input type="password"
name="password" />
</form>
</body>
</html>
MULTI-LINE TEXT INPUT CONTROLS
• This is used when the user is required to give
details that may be longer than a single sentence.
• Multi-line input controls are created using
HTML <textarea> tag.
EXAMPLE
<html>
<head>
<title>Multiple-Line Input Control</title>
</head><body>
<form>
Description : <br />
<textarea rows="5" cols="50" name="description">
Enter description here...
</textarea>
</form>
</body>
</html>
TEXTAREA ATTRIBUTES
• Following is the list of attributes for
<textarea> tag.
CHECKBOX CONTROL
• Checkboxes are used when more than one option is
required to be selected.
• They are also created using HTML <input> tag but type
attribute is set to checkbox.
CHECKBOX ATTRIBUTES
EXAMPLE
<html>
<head>
<title>Checkbox Control</title>
</head>
<body>
<form>
<input type="checkbox" name="maths" value=“Maths">
Maths
<input type="checkbox" name="physics" value=“Physics">
Physics
</form>
</body>
</html>
RADIO BUTTON CONTROL
• Radio buttons are used when out of many options, just
one option is required to be Selected.
• They are also created using HTML <input> tag but type
attribute is set to radio.
RADIO BUTTON ATTRIBUTES
• Following is the list of attributes for radio
button:
EXAMPLE
<html>
<head>
<title>Radio Box Control</title>
</head>
<body>
<form>
<input type="radio" name="subject" value="maths"> Maths
<input type="radio" name="subject" value="physics"> Physics
</form>
</body>
</html>
SELECT BOX CONTROL
• A select box, also called drop down box which provides option to
list down various options in the form of drop down list, from where
a user can select one or more options.
<html>
<head>
<title>Select Box Control</title>
</head>
<body>
<form>
<select name="dropdown">
<option value="Maths" selected>Maths</option>
<option value="Physics">Physics</option>
</select>
</form>
</body>
</html>
SELECT BOX ATTRIBUTES
FILE UPLOAD CONTROL
• If you want to allow a user to upload a file to your
web site, you will need to use a file upload box,
also known as a file select box.
• This is also created using the <input> element but
type attribute is set to file.
FILE UPLOAD ATTRIBUTES
EXAMPLE
<html>
<head>
<title>File Upload Box</title>
</head>
<body><form>
Select file:
<input type="file" name="fileupload" accept="image/*" />
</form>
</body>
</html>
BUTTON CONTROL
• There are various ways in HTML to create
clickable buttons.
• You can also create a clickable button using
<input> tag by setting its type attribute to
button.
BUTTON CONTROL…
EXAMPLE
<html>
<head>
<title>Buttons</title>
</head>
<body>
<form>
<input type="submit" name="submit" value="Submit" />
<input type="reset" name="reset" value="Reset" />
<input type="button" name="ok" value="OK" />
<input type="image" name="imagebutton" src="images/logo.jpg“
alt=“submit” />
</form>
</body>
</html>