2HTML Forms Latest1
2HTML Forms Latest1
It is a language used for presenting results with good look and feel.
Browsers do not display the HTML tags, but use them to render the content of the page
1) Document Type
2) Title
3) Head
4) Body
HTML Basics:
HTML Documents
The HTML document itself begins with <html> and ends with </html>.
The visible part of the HTML document is between <body> and </body>.
Eg:
<!DOCTYPE HTML>
<html>
<head>
</head>
</html>
HTML Comments
HTML Headings
Eg:
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
</body>
</html>
<!- -
This is
Multiple lines
commenting
- ->
Eg:
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<!--
-->
</body>
</html>
HTML Paragraphs
Eg:
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<p>
<h1>
My first paragraph.
</h1>
</p>
<p>
<h2>
My second paragraph.
</h2>
</p>
</body>
</html>
Eg:
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<p>
<h1>
My first paragraph.<br>
</h1>
</p>
<p>
<h2>
</h2>
</p>
</body>
</html>
HTML Forms:
Forms are user interface, which is used to collect user input.
Form elements are different types of input elements, like text fields,
checkboxes, radio buttons, submit buttons, and more.
<input type="button">
<input type="checkbox">
<input type="color">
<input type="date">
<input type="datetime-local">
<input type="email">
<input type="file">
<input type="hidden">
<input type="image">
<input type="month">
<input type="password">
<input type="radio">
<input type="range">
<input type="reset">
<input type="search">
<input type="submit">
<input type="tel">
<input type="text">
<input type="time">
<input type="url">
<input type="week">
If the type attribute is omitted, the input field gets the default
type: "text".
Eg:
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<form >
</form>
</body>
</html>
Text Input
<input type="text"> defines a one-line input field for text input:
Eg:
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<form>
</form>
</body>
</html>
Result:
Text filed attributes:
Name attribute.
Value attribute .
Eg:
<!DOCTYPE HTML>
<html>
<head>
<title>Rigistration form </title>
</head>
<body>
<h1> Rigistration Form </h1>
<form >
First Name <input type = "text" name = "fname" value = "Nageswar Rao"/> <br>
Last Name <input type = "text" name = "lname" value = "Mandru"/> <br>
</form>
</body>
</html>
Radio Button Input
<input type="radio"> defines a radio button.
Radio buttons let a user select ONE of a limited number of choices ( single
selection )
Eg:
<!DOCTYPE html>
<html>
<body>
<h2>Radio Buttons</h2>
<form>
</form>
</body>
</html>
Description for the above:
" name="gender" it must be same for all as all radio buttons are under a single group.
Male<br> it is a label.
<!DOCTYPE html>
<html>
<body>
<form action="">
<br><br>
</form>
</body>
</html>
Default selection: checked
<!DOCTYPE html>
<html>
<body>
<form action="">
<br><br>
</form>
</body>
</html>
The Submit Button
<input type="submit"> defines a button for submitting the form data to
a form-handler.
The form-handler is typically a server page with a script for processing input
data.
Eg:
<!DOCTYPE HTML>
<html>
<head>
<title>Rigistration form </title>
</head>
<body>
<h1> Rigistration Form </h1>
<form >
First Name <input type = "text" name = "fname" value = "Nageswar Rao"/> <br>
Last Name <input type = "text" name = "lname" value = "Mandru"/> <br>
</body>
</html>
Normally, the form data is sent to a web page on the server when the user
clicks on the submit button.
In the example above, the form data is sent to a page on the server called
"/registrationDetails ". This page contains a server-side script that handles the
form data:
If the action attribute is omitted, the action is set to the current page.
The Target Attribute
The target attribute specifies if the submitted result will open in a new browser
tab, a frame, or in the current window.
The default value is "_self" which means the form will be submitted in the
current window.
To make the form result open in a new browser tab, use the value " _blank":
Other legal values are "_parent", "_top", or a name representing the name of
an iframe.
" target="_self"
Eg:
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
Last Name <input type = "text" name = "lname" value = "Mandru"/> <br>
</form>
</body>
</html>
Target = _blank
Eg:
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
First Name <input type = "text" name = "fname" value = "Nageswar Rao"/> <br>
Last Name <input type = "text" name = "lname" value = "Mandru"/> <br>
<input type="submit" value="Submit">
</form>
</body>
</html>
Using metho=”GET”
Eg:
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
</form>
</body>
</html>
file:///D:/NRIT_HTML/test.htm ? fname=Nageswar+Rao&lname=Mandru
fname=Nageswar+Rao&lname=Mandru
Using method=”POST”
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
</form>
</body>
</html>
Notes on GET:
Always use POST if the form data contains sensitive or personal information.
The POST method does not display the submitted form data in the page address
field.
Notes on POST:
• POST has no size limitations, and can be used to send large amounts of
data.
It depends on your application need. If client data includes only ASCII characters, no secrecy
and limited to 2KB length (depends on the browser), then prefer GET, else POST
Eg:
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<form action="">
First Name <input type = "text" name = "fname" value = "Nageswar Rao"/> <br>
Last Name <input type = "text" name = "lname" value = "Mandru"/> <br>
</form>
</body>
</html>
Input Type Reset
<input type="reset"> defines a reset button that will reset all form values to
their default values:
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<form action="">
First Name <input type = "text" name = "fname" value = "Nageswar Rao"/> <br>
Last Name <input type = "text" name = "lname" value = "Mandru"/> <br>
</form>
</body>
</html>
The <select> Element
The <select> element defines a drop-down list:
<!DOCTYPE html>
<html>
<body>
<form action="">
<select name="category">
<option value="Doctor">Doctor</option>
<option value="Lawyer">Lawyer</option>
</select>
<br><br>
<input type="submit">
</form>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<form action="">
<select name="category">
<option value="Doctor">Doctor</option>
<option value="Lawyer">Lawyer</option>
</select>
<br><br>
<input type="submit">
</form>
</body>
</html>
Visible Values:
Use the size attribute to specify the number of visible values:
Eg:
<!DOCTYPE html>
<html>
<body>
<form action="">
<option value="Doctor">Doctor</option>
<option value="Lawyer">Lawyer</option>
</select>
<br><br>
<input type="submit">
</form>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<form action="">
<option value="pickle">Pickle</option>
<option value="bannana">Bannana</option>
</select>
<br><br>
<input type="submit">
</form>
</body>
</html>
The <textarea> Element
The <textarea> element defines a multi-line input field (a text area):
Eg:
<!DOCTYPE html>
<html>
<body>
<form action="">
<input type="submit">
</form>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<form action="">
</form>
</body>
</html>
color
date
datetime-local
email
month
number
range
search
tel
time
url
week
Depending on browser support, a color picker can show up in the input field.
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form action="">
</form>
</body>
</html>
Input Type Date
The <input type="date"> is used for input fields that should contain a date.
Depending on browser support, a date picker can show up in the input field.
<!DOCTYPE html>
<html>
<body>
<form action="">
</form>
</body>
</html>
You can also use the min and max attributes to add restrictions to dates:
<!DOCTYPE html>
<html>
<body>
<form action="">
<input type="submit">
</form>
</body>
</html>
Input Type Datetime-local
The <input type="datetime-local"> specifies a date and time input field, with
no time zone.
Depending on browser support, a date picker can show up in the input field.
<!DOCTYPE html>
<html>
<body>
<form action="">
</form>
</body>
</html>
Input Type Email
The <input type="email"> is used for input fields that should contain an e-
mail address.
Some smartphones recognize the email type, and add ".com" to the keyboard to
match email input.
<!DOCTYPE HTML>
<html>
<head>
<title>Registration Form</title>
</head>
<body>
<form action="/action_page.php">
E-mail:
<input type="email" name="email">
<input type="submit">
</form>
</body>
</html>
Depending on browser support, a date picker can show up in the input field.
<!DOCTYPE HTML>
<html>
<head>
<title>Registration Form</title>
</head>
<body>
<form action="">
<input type="submit">
</form>
</body>
</html>
The following example displays a numeric input field, where you can enter a
value from 1 to 5:
<head>
<title>Registration Form</title>
</head>
<body>
<form action="">
<input type="submit">
</form>
</body>
</html>
Input Restrictions
Here is a list of some common input restrictions (some are new in HTML5):
Attribute Description
min,max,step,value
The following example displays a numeric input field, where you can enter a
value from 0 to 100, in steps of 10. The default value is 30:
<head>
<title>Registration Form</title>
</head>
<body>
<form action="">
Quantity:
<input type="submit">
</form>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h2>Range Field</h2>
<p>Depending on browser support:<br>The input type "range" can be displayed as a slider control.<p>
<!DOCTYPE HTML>
<html>
<head>
<title>Registration Form</title>
</head>
<body>
Points:
<input type="submit">
</form>
</body>
</html>
<html>
<head>
<title>Registration Form</title>
</head>
<body>
<form action="/action_page.php">
Search Google:
<input type="submit">
</form>
</body>
</html>
<!DOCTYPE HTML>
<html>
<head>
<title>Registration Form</title>
</head>
<body>
<form action="/action_page.php">
Telephone:
<input type="submit">
</form>
</body>
</html>
Input Type Time
The <input type="time"> allows the user to select a time (no time zone).
Depending on browser support, a time picker can show up in the input field.
<!DOCTYPE HTML>
<html>
<head>
<title>Registration Form</title>
</head>
<body>
<form action="/action_page.php">
Select a time:
<input type="submit">
</form>
</body>
</html>
Some smartphones recognize the url type, and adds ".com" to the keyboard to
match url input.
<!DOCTYPE HTML>
<html>
<head>
<title>Registration Form</title>
</head>
<body>
<form action="">
<input type="submit">
</form>
</body>
</html>
Depending on browser support, a date picker can show up in the input field.
<!DOCTYPE HTML>
<html>
<head>
<title>Registration Form</title>
</head>
<body>
<form action="/action_page.php">
Select a week:
<input type="submit">
</form>
</body>
</html>
HTML5 Form Elements
HTML5 added the following form elements:
<datalist>
<output>
Note: Browsers do not display unknown elements. New elements that are not
supported in older browsers will not "destroy" your web page.
Users will see a drop-down list of the pre-defined options as they input data.
The list attribute of the <input> element, must refer to the id attribute of
the <datalist> element.
<html>
<body>
<p>The datalist element specifies a list of pre-defined options for an input element.</p>
<form action="/action_page.php">
<datalist id="browsers">
<option value="Firefox">
<option value="Chrome">
<option value="Opera">
<option value="Safari">
</datalist>
<input type="submit">
</form>
<p><b>Note:</b> The datalist tag is not supported in Safari or IE9 (and earlier).</p>
</body>
</html>
Example
Perform a calculation and show the result in an <output> element:
<!DOCTYPE html>
<html>
<body>
<form action="/action_page.php"
oninput="x.value=parseInt(a.value)+parseInt(b.value)">
100 +
<br><br>
<input type="submit">
</form>
</body>
</html>