Class-5A-HTMLForm Elements-Form Attributes
Class-5A-HTMLForm Elements-Form Attributes
HTML Forms
An HTML form is used to collect user input. The user input can then be sent to a server for
processing.
HTML forms are used to send the user information to the server and returns the result back to the
browser. For example, if you want to get the details of visitors to your website, and send them
some message, you can collect the user information by means of form processing. Then, the
information can be validated either at the client-side or on the server-side. The final result is sent
to the client through the respective web browser. To create a HTML form, form tag should be
used.
Example
First name:
John
Last name:
Doe
Submit
Example
<input type="text" id="firstname" name="firstname">
If the type attribute is omitted, the input field gets the default type: "text".
Attributes of input element
id attribute The id attribute is a unique identifier of the HTML element. Each id attribute must
be unique. Also, this attribute must begin with a letter and is case sensitive. It can be used as an
anchor reference in URL. It isn’t associated with the data within the element.
In CSS, the id attribute is referenced with the # character. In Javascript, it is referenced with
getElementById().
name attribute The name attribute defines a name of the element. It is used in the HTTP
request that is sent to the server as a variable name by the browser. This attribute is associated
with the data within the element.
Like the id attribute, the name attribute must begin with a letter and is case sensitive, but unlike
the id attribute, it can be not unique.
The name attribute cannot be referenced in CSS. In Javascript, it is referenced with
getElementsByName().
This attribute is valid only on the following
elements: <button>, <fieldset>, <form>, <iframe>, <input>, <map>, <meta>, <object>, <output
>, <param>, <select>, and <textarea>.
Text Fields
<input type="text"> defines a single-line input field for text input.
Example
HTML Notes Page No.3
Last name:
Note: The form itself is not visible. Also note that the default width of an input field is 20
characters.
Radio Buttons
<input type="radio"> defines a radio button.
Radio buttons let a user select ONE of a limited number of choices.
Example
A form with radio buttons:
<p>Choose your Gender : </p>
<form>
<input type="radio" id="male" name="gender" value="male">
<label for="male">Male</label><br>
<input type="radio" id="female" name="gender" value="female">
<label for="female">Female</label><br>
<input type="radio" id="other" name="gender" value="other">
<label for="other">Other</label>
</form>
This is how the HTML code above will be displayed in a browser:
The for attribute of the <label> tag should be equal to the id attribute of the <input> element to
bind them together.
Checkboxes
The <input type="checkbox"> defines a checkbox.
Checkboxes let a user select ZERO or MORE options of a limited number of choices.
Example
A form with checkboxes:
<form>
<input type="checkbox" id="vehicle1" name="vehicle1" value="Bike">
<label for="vehicle1"> I have a bike</label><br>
<input type="checkbox" id="vehicle2" name="vehicle2" value="Car">
<label for="vehicle2"> I have a car</label><br>
<input type="checkbox" id="vehicle3" name="vehicle3" value="Boat">
<label for="vehicle3"> I have a boat</label>
</form>
This is how the HTML code above will be displayed in a browser:
I have a bike
I have a car
I have a boat
The <date> element
The <input type="date"> defines a date picker.
The resulting value includes the year, month, and day.
Always add the <label> tag for best accessibility practices!
Example
Show a date control:
<label for="birthday">Birthday:</label>
<input type="date" id="birthday" name="birthday">
Visible Values:
Use the size attribute to specify the number of visible values:
Example
<select name="courses" size="3">
<option value="pgdca">PGDCA</option>
<option value="dca">DCA</option>
<option value="dcas" selected>DCA-S</option>
<option value="tally">TALLY</option>
</select>
Allow Multiple Selections:
Use the multiple attribute to allow the user to select more than one value:
Example
<select name="courses" size="6" multiple>
<option value="pgdca">PGDCA</option>
<option value="dca">DCA</option>
<option value="dcas" selected>DCA-S</option>
<option value="tally">TALLY</option>
<option value="deoa">DEOA</option>
<option value="python">PYTHON</option>
<option value="oracle">ORACLE</option>
<option value="advancedExcel">Advanced Excel</option>
</select>
The <textarea> Element
The <textarea> element defines a multi-line input field (a text area):
Example
<textarea name="message" rows="10" cols="30">
The cat was playing in the garden.
</textarea>
The rows attribute specifies the visible number of lines in a text area.
The cols attribute specifies the visible width of a text area.
This is how the HTML code above will be displayed in a browser:
You can also define the size of the text area by using CSS:
Example
<textarea name="message" style="width:200px; height:600px;">
The cat was playing in the garden.
</textarea>
The <button> Element
The <button> element defines a clickable button:
HTML Notes Page No.6
Example
<button type="button" onclick="alert('Hello World!')">Click Me!</button>
This is how the HTML code above will be displayed in a browser:
Click Me!
Note: Always specify the type attribute for the button element. Different browsers may use
different default types for the button element.
Last name:
Manohar
Submit
Example
<form action="/action_page.php">
<input list="browsers">
<datalist id="browsers">
<option value="Internet Explorer">
<option value="Firefox">
<option value="Chrome">
<option value="Opera">
<option value="Safari">
HTML Notes Page No.7
</datalist>
</form>
The <output> Element
The <output> element represents the result of a calculation (like one performed by a script).
Example
Perform a calculation and show the result in an <output> element:
<form action="/action_page.php"
oninput="x.value=parseInt(a.value)+parseInt(b.value)">
0
<input type="range" id="a" name="a" value="50">
100 +
<input type="number" id="b" name="b" value="50">
=
<output name="x" for="a b"></output>
<br><br>
<input type="submit">
</form>
The Submit Button
<input type="submit"> defines a button for submitting the form data to a form-handler.
The form-handler is typically a page on the server with a script for processing input data.
The form-handler is specified in the form's action attribute.
Example
A form with a submit button:
<form action="/action_page.php">
<label for="fname">First name:</label><br>
<input type="text" id="fname" name="fname" value="ANAND"><br>
<label for="lname">Last name:</label><br>
<input type="text" id="lname" name="lname" value="KUMAR"><br><br>
<input type="submit" value="Submit">
</form>
This is how the HTML code above will be displayed in a browser:
First name:
ANAND
Last name:
KUMAR
Submit
<!DOCTYPE html>
<html>
<head>
<title>Simple Form Processing</title>
</head>
<body>
<form id="form1" method="post">
FirstName:
<input type="text" name="firstname" required/>
<br>
<br>
LastName
<input type="text" name="lastname" required/>
<br>
<br>
Address
HTML Notes Page No.10
<?php
if (isset($_POST['submit']))
{
if ((!isset($_POST['firstname'])) || (!isset($_POST['lastname'])) ||
(!isset($_POST['address'])) || (!isset($_POST['emailaddress'])) ||
(!isset($_POST['password'])) || (!isset($_POST['gender'])))
{
$error = "*" . "Please fill all the required fields";
}
else
{
$firstname = $_POST['firstname'];
HTML Notes Page No.11
$lastname = $_POST['lastname'];
$address = $_POST['address'];
$emailaddress = $_POST['emailaddress'];
$password = $_POST['password'];
$gender = $_POST['gender'];
}
}
?>
<html>
<head>
<title>Simple Form Processing</title>
</head>
<body>
<h1>Form Processing using PHP</h1>
<fieldset>
<form id="form1" method="post" action="form.php">
<?php
if (isset($_POST['submit']))
{
if (isset($error))
{
echo "<p style='color:red;'>"
. $error . "</p>";
}
}
?>
FirstName:
<input type="text" name="firstname"/>
<span style="color:red;">*</span>
<br>
<br>
Last Name:
<input type="text" name="lastname"/>
<span style="color:red;">*</span>
<br>
<br>
Address:
<input type="text" name="address"/>
<span style="color:red;">*</span>
<br>
<br>
Email:
HTML Notes Page No.12
</html>
Output:
Note: When the PHP and HTML are coded in a single file, the file should be saved as PHP. In
the form, the value for the action parameter should be a file name.
HTML Notes Page No.14
Example
<h1 style="background-color:DodgerBlue;">Hello World</h1>
<p style="background-color:Tomato;">Lorem ipsum...</p>
Text Color
You can set the color of text:
Hello World
Example
<h1 style="color:Tomato;">Hello World</h1>
<p style="color:DodgerBlue;">Lorem ipsum...</p>
<p style="color:MediumSeaGreen;">Ut wisi enim...</p>
Border Color
You can set the color of borders:
Hello World
Hello World
Hello World
Example
<h1 style="border:2px solid Tomato;">Hello World</h1>
<h1 style="border:2px solid DodgerBlue;">Hello World</h1>
<h1 style="border:2px solid Violet;">Hello World</h1>
Color Values
In HTML, colors can also be specified using RGB values, HEX values, HSL values, RGBA
values, and HSLA values.
The following three <div> elements have their background color set with RGB, HEX, and HSL
values:
rgb(255, 99, 71)
HTML Notes Page No.15
#ff6347
hsl(9, 100%, 64%)
The following two <div> elements have their background color set with RGBA and HSLA
values, which adds an Alpha channel to the color (here we have 50% transparancy):
rgba(255, 99, 71, 0.5)
hsla(9, 100%, 64%, 0.5)
Example
<h1 style="background-color:rgb(255, 99, 71);">...</h1>
<h1 style="background-color:#ff6347;">...</h1>
<h1 style="background-color:hsl(9, 100%, 64%);">...</h1>
<h1 style="background-color:rgba(255, 99, 71, 0.5);">...</h1>
<h1 style="background-color:hsla(9, 100%, 64%, 0.5);">...</h1>