0% found this document useful (0 votes)
20 views

HTML Form

Uploaded by

Mudit Rajput
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

HTML Form

Uploaded by

Mudit Rajput
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 37

HTML Forms

ABHISHEK SINGH

05/28/2024 1
HTML Forms
The <form> Element
The HTML <form> element defines a form that is used to collect user input:

<form>
.
<!-- form elements
Form elements are different types of input elements, like text fields,
checkboxes, radio buttons, submit buttons, and more.

-->

.
</form>
HTML Forms (Attributes )
The Action Attribute
The action attribute defines the action to be performed when the form is
submitted. Normally, the form data is sent to a web page on the server when the user
clicks on the submit button.

<form action="action_page.php">

If the action attribute is omitted, the action is set to the current page.
HTML Forms (Attributes )
The Method Attribute
The method attribute specifies the HTTP method (GET or POST) to be used when
submitting the form data:

<form action="action_page.php" method="get">


or
<form action="action_page.php" method="post">
HTML Forms (Attributes )
When to Use GET?
The default method when submitting form data is GET. However, when GET is
used, the submitted form data will be visible in the page address field:

action_page.php?firstname=Mickey&lastname=Mouse

When to Use POST?


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. POST has no size limitations, and can be used to send large
amounts of data.

action_page.php
HTML Forms (Attributes )
The Name Attribute
Each input field must have a name attribute to be submitted.
If the name attribute is omitted, the data of that input field will not be sent at all.
This example will only submit the "Last name" input field:
Example

<form action="action_page.php">
First name:<br>
<input type="text" value="Mickey"><br>
Last name:<br>
<input type="text" name="lastname" value="Mouse">
<br><br>
<input type="submit" value="Submit">
</form>
HTML Forms (Attributes )
Grouping Form Data with <fieldset>
The <fieldset> element is used to group related data in a form.
The <legend> element defines a caption for the <fieldset> element.

Example
<form action="action_page.php">
<fieldset>
<legend>Personal information:</legend>
First name:<br>
<input type="text" name="firstname" value="Mickey"><br>
Last name:<br>
<input type="text" name="lastname" value="Mouse"><br>
<br>
<input type="submit" value="Submit">
</fieldset>
</form>
HTML Forms (Attributes )
Attribute Description
accept-charset Specifies the charset used in the submitted form (default:
the page charset).
action Specifies an address (url) where to submit the form
(default: the submitting page).
autocomplete Specifies if the browser should autocomplete the form
(default: on).
enctype Specifies the encoding of the submitted data (default: is
url-encoded).
method Specifies the HTTP method used when submitting the
form (default: GET).
name Specifies a name used to identify the form (for DOM
usage: document.forms.name).
novalidate Specifies that the browser should not validate the form.

target Specifies the target of the address in the action attribute


(default: _self).
HTML Form Elements
The <input> Element

The <select> Element

The <textarea> Element

The <button> Element

HTML 5 Form Elements


<datalist> <keygen> <output>
Note: Browsers do not display unknown elements. New elements that are not supported
in older browsers will not "destroy" your web page.
HTML Form Elements
HTML Input Types
Input Type Text

<input type="text"> defines a one-line text input field:

Example

<form>
First name:
<br>
<input type="text“ name="firstname“ ><br>
Last name:
<br>
<input type="text" name="lastname">

</form>
HTML Form Elements
HTML Input Types
Input Type Password

<input type="text"> defines a one-line Password field :

Example

<form>
First name:
<br>
<input type="text“ name="firstname“ >
<br>
Password:
<br>
<input type=“password" name=“password">

</form>
HTML Form Elements
HTML Input Types
Input Type Submit

<input type="submit"> defines a button for submitting form data to


a form-handler.The form-handler is typically a server page with a script for
processing input data.
The form-handler is specified in the form's action attribute:

Example
<form action="action_page.php">

First name:<br>
<input type="text" name="firstname" value="Mickey"><br>
Last name:<br>
<input type="text" name="lastname" value="Mouse"><br><br>
<input type="submit" value="Submit">
</form>
HTML Form Elements
HTML Input Types
Input Type Reset

<input type="reset"> defines a reset button that will reset all form values to
their default values:

Example
<form action="action_page.php">
First name:<br>
<input type="text" name="firstname" value="Mickey"><br>
Last name:<br>
<input type="text" name="lastname" value="Mouse"><br><br>
<input type="submit" value="Submit">
<input type="reset">

</form>
HTML Form Elements
HTML Input Types

Input Type Radio

<input type="radio"> defines a radio button.


Radio buttons let a user select ONLY ONE of a limited number of choices:

Example

<form>

<input type="radio" name="gender" value="male" checked> Male<br>


<input type="radio" name="gender" value="female"> Female<br>
<input type="radio" name="gender" value="other"> Other

</form>
HTML Form Elements
HTML Input Types
Input Type Checkbox

<input type="checkbox"> defines a checkbox.


Checkboxes let a user select ZERO or MORE options of a limited number of choices.

Example

<form>

<input type="checkbox" name="vehicle1" value="Bike"> I have a bike<br>


<input type="checkbox" name="vehicle2" value="Car"> I have a car

</form>
HTML Form Elements
HTML Input Types
Input Type Button
<input type="button"> defines a button:

Example

<input type="button" onclick="alert('Hello World!')" value="Click Me!">


HTML Form Elements
HTML 5 Input Types
•datalist
•color
• date
• datetime
• datetime-local
• email
• month
• number
• range
• search
• tel
• time
• url
• week
HTML Form Elements
HTML5 <datalist> Element
The <datalist> element specifies a list of pre-defined options for an <input>
element.
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.
<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">
</datalist>
</form>
Note: Browsers do not display unknown elements. New elements that are not supported
in older browsers will not "destroy" your web page.
HTML Form Elements
HTML 5 Input Types
Input Type Number
The <input type="number"> defines a numeric input field.
You can also set restrictions on what numbers are accepted.
The following example displays a numeric input field, where you can enter a value
from 1 to 5:Example

<form>

Quantity (between 1 and 5):

<input type="number" name="quantity" min="1" max="5">

</form>
HTML Form Elements
HTML 5 Input Types

<form>
Quantity:

<input type="number" name="points" min="0" max="100" step="10" value="30">

</form>
HTML Form Elements
HTML 5 Input Types
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.

<form>
Birthday:
<input type="date" name="bday">
</form>
<form>
Enter a date before 1980-01-01:
<input type="date" name="bday" max="1979-12-31"><br>
Enter a date after 2000-01-01:
<input type="date" name="bday" min="2000-01-02"><br>
</form>
HTML Form Elements
HTML 5 Input Types
Input Type Color
The <input type="color"> is used for input fields that should contain a color.
Depending on browser support, a color picker can show up in the input field.

<form>

Select your favorite color:


<input type="color" name="favcolor">

</form>
Example

HTML Form Elements


HTML 5 Input Types
Input Type Range

The <input type="range"> is used for input fields that should contain a value
within a range.
Depending on browser support, the input field can be displayed as a slider
control.

<form>

<input type="range" name="points" min="0" max="10">

</form>
HTML Form Elements
HTML 5 Input Types
Input Type Month
The <input type="month"> allows the user to select a month and year.
Depending on browser support, a date picker can show up in the input field.

<form>

Birthday (month and year):


<input type="month" name="bdaymonth">

</form>
HTML Form Elements
HTML 5 Input Types
Input Type Week
The <input type="week"> allows the user to select a week and year.
Depending on browser support, a date picker can show up in the input field.

<form>

Select a week:
<input type="week" name="week_year">

</form>
HTML Form Elements
HTML 5 Input Types
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.

<form>

Select a week:
<input type=“time" name=“user time">

</form>
HTML Form Elements
HTML 5 Input Types
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.

<form>

Birthday (date and time):


<input type="datetime-local" name="bdaytime">

</form>
HTML Form Elements
HTML 5 Input Types
Input Type Email
The <input type="email"> is used for input fields that should contain an e-mail
address.
Depending on browser support, the e-mail address can be automatically validated
when submitted.
Some smartphones recognize the email type, and adds ".com" to the keyboard to
match email input.

<form>

E-mail:
<input type="email" name="email">

</form>
HTML Form Elements
HTML 5 Input Types

Input Type Search


The <input type="search"> is used for search fields (a search field behaves like a
regular text field).

<form>
Search Google:
<input type="search" name="googlesearch">
</form>
HTML Form Elements
HTML 5 Input Types
Input Type Tel
The <input type="tel"> is used for input fields that should contain a telephone
number.
The tel type is currently supported only in Safari 8.

<form>
Telephone:
<input type="tel" name="usrtel">
</form>
HTML Form Elements
HTML 5 Input Types

Input Type Url


The <input type="url"> is used for input fields that should contain a URL address.
Depending on browser support, the url field can be automatically validated when
submitted.
Some smartphones recognize the url type, and adds ".com" to the keyboard to
match url input.

<form>
Add your homepage:
<input type="url" name="homepage">
</form>
HTML Form Elements
HTML Input Types(Input Restriction)
Attribute Description
disabled Specifies that an input field should be disabled
max Specifies the maximum value for an input field
maxlength Specifies the maximum number of character for an input field
min Specifies the minimum value for an input field
pattern Specifies a regular expression to check the input value against
readonly Specifies that an input field is read only (cannot be changed)
required Specifies that an input field is required (must be filled out)
size Specifies the width (in characters) of an input field
step Specifies the legal number intervals for an input field
value Specifies the default value for an input field
HTML Form Elements
HTML 5 Input Types(Input Attributes Restriction)

The readonly Attribute

The readonly attribute specifies that the input field is read only (cannot be changed):

Example

<form action="">
First name:<br>
<input type="text" name="firstname" value="John" readonly>
</form>
HTML Form Elements
HTML 5 Input Types(Input Attributes Restriction)

The disabled Attribute


The disabled attribute specifies that the input field is disabled.
A disabled input field is unusable and un-clickable, and its value will not be sent
when submitting the form:

Example
<form action="">
First name:<br>
<input type="text" name="firstname" value="John" disabled>
</form>
HTML Form Elements
HTML 5 Input Types(Input Attributes Restriction)

The size Attribute

The size attribute specifies the size (in characters) for the input field:

<form action="">
First name:<br>
<input type="text" name="firstname" value="John" size="40">
</form>
HTML Form Elements
HTML 5 Input Types(Input Attributes Restriction)

The Max Length

The maxlength attribute specifies the maximum allowed length for input
field.

<form action="">
First name:<br>
<input type="text" name="firstname" value="John" maxlength="40">
</form>
THANKS

05/28/2024 37

You might also like