HTML Forms
-
This lesson explains how to create and use HTML forms to collect user input using form tags, input fields, labels, and buttons.
๐ Why Forms Are the Heart of the Web
Imagine the web without forms โโ No login, no signup, no search box, no shopping cart, no paymentsโฆ nothing interactive.
Forms make websites come alive. They enable:
โจ ๐ Authentication (login / signup)
๐ Every dynamic web application depends on forms.
โจ ๐ Search & filters (YouTube search, Amazon filters)
โจ ๐๏ธ Add to cart + checkout (shopping websites)
โจ ๐ณ Online payments (UPI, card, wallets)
โจ ๐ฉ Communication (contact forms, feedback)
Forms are the bridge between users โ browsers โ servers.
๐ก Imagine you are logging into Instagram
You type:
Username
Password
โ Hit Login
This HTML runs in the background:
<form action="/login" method="POST">
<label for="user">Username</label>
<input type="text" id="user" name="username" required>
<label for="pass">Password</label>
<input type="password" id="pass" name="password" minlength="8">
<button type="submit">Login</button>
</form>
Behind the scenes:
Input Key Sent to Server Value Username username your typed username Password password your password
Server checks:
Is user registered ?
Does the password match ?
Is the account secure ?
Then shows:
โ Login success โ Take user to dashboard
โ Login failed โ Show error messageHow a Form Works
๐ค User types data
โ
๐งฑ <form> collects everything
โ
๐ Converts to name=value pairs
โ
๐ Sends to server (GET/POST)
โ
๐ฅ๏ธ Server processes request
โ
๐ Sends result back to user
๐ฏ Analogy
Think of a pizza order form ๐:
You choose size โ input
You choose toppings โ checkbox
You write address โ textarea
You click Order โ submit button
Then:
Order goes to kitchen โ server
Pizza gets prepared โ processing
Delivered to your home โ response
๐งฑ Full Form Anatomy
Below is a complete login form. This is the heart of every web application:
<form action="/login" method="POST">
<label for="user">Username</label>
<input type="text" id="user" name="username" required>
<label for="pass">Password</label>
<input type="password" id="pass" name="password" minlength="8">
<button type="submit">Login</button>
</form>
๐ Understanding the <form> Tag
โ Role of a Form
A form is like a container that holds all user inputs.
โ Collects data
โ Groups inputs together
โ Defines what happens when user clicks Submit
โ Connects the browser to the backendYou can imagine it like a delivery box ๐ฆ
Inputs = items inside box
Submit button = sends box
action = where box is delivered
method = how delivery is done
๐๏ธ Important Form Attributes
Attribute Meaning action Where data is sent (URL) method How data is sent (GET/POST) target Opens response in new tab or same tab autocomplete Enables/Disables autofill novalidate Turns off browser validation
๐น method="GET"
<form method="GET">
๐ง How GET Works
Data is added in URL as a query string
Example:
/search?q=html&page=1
โญ Best For:
โ Search
โ Filters
โ Bookmarkable pagesโ Problems:
โ Data is visible
โ Limited length
โ Not secure
๐น method="POST"
<form method="POST">
๐ง How POST Works
Sends data in the request body
Hidden from URL
No size limits
โญ Best For:
โ Login / Signup
๐ฅ Always use POST when sending sensitive data.
โ Passwords
โ Payments
โ File uploads
- ๐ท๏ธ <label>
Email Input Field with Label
This code creates an email input field with a label, allowing users to enter their email address in a clear and accessible way.
<form>
<label for="email">Email</label>
<input id="email" type="email">
</form>
๐ก Why <label> is Important:
โ Clicking the label focuses the input
โ Helps blind users (screen readers)
โ Required for proper accessibilityโ Bad
<label>Email</label><input>
โ Good
<label for="e">Email</label>
๐งพ <input>
Form with Text Input
This code shows a simple HTML form containing a text input field, used to collect basic user input.
<form>
<input type="text">
</form>
Inputs can collect:
names
emails
passwords
numbers
dates
files
colors
ranges
Buttons
๐ข Common Input Types Explained
๐ค text
Single-line input
Example: Username, Full Name๐ง email
<input type="email">
โ Validates email format
โ Shows correct keyboard on mobile๐ password
โ Hides characters
Does NOT encrypt โ needs HTTPS๐ข number
<input type="number">
โ Only allows numbers
โ Adds up/down arrows๐ date
<input type="date">
โ Shows calendar picker
๐ file
<input type="file">
Used for uploading:
images
pdf
videos
โ ๏ธ Must include:
enctype="multipart/form-data"
๐งฉ Input Attributes
๐ If there is NO name, the data is NOT sent to the server.Attribute Meaning name Key used by server value Default value placeholder Hint inside input required Must be filled readonly Cannot edit but visible disabled Can't edit & not submitted pattern Regex validation min / max Range control ๐ <textarea> (For Long Messages)
Textarea Input Field in HTML Form
This code creates a textarea inside a form, allowing users to enter multi-line text such as comments or messages.
<form>
<textarea rows="4" cols="40"> </textarea>
</form>
Best for:
feedback
reviews
comments
messages
Supports:
maxlength
readonly
required
๐ฝ <select> Dropdown โ Smart Input
Dropdown Menu Using Select Tag
This code creates a dropdown list inside a form that allows users to choose a course such as HTML or CSS.
<form>
<select name="course">
<option value="html">HTML</option>
<option value="css">CSS</option>
</select>
</form>
โ Only one value is sent
โ selected makes it default๐ Radio Buttons โ Choose Only One
Radio Button Group Example
This form uses radio buttons to let users select one option Male or Female from a single choice group.
<form>
<input type="radio" name="gender" value="m"> Male
<input type="radio" name="gender" value="f"> Female
</form>
๐ก Same name = only one can be selected
โ๏ธ Checkboxes โ Choose Many
Checkbox Input Field in HTML Form
This code shows a checkbox inside an HTML form, commonly used to accept terms and conditions, with the checkbox pre-selected by default.
<form>
<input type="checkbox" name="terms" checked>
</form>
โ Can check multiple
โ Each sends its value๐ Submit Buttons
Submit Button in HTML Form
This code creates a submit button inside a form, which sends the form data when clicked.
<form>
<button type="submit">Send</button>
</form>
Triggers form submission.
Multiple buttons = different actions.
Multiple Form Actions Using Buttons
This example shows how different buttons in a form can submit data to different URLs using the formaction attribute.
<form>
<button formaction="/save">Save</button>
<button formaction="/publish">Publish</button>
</form>
๐ HTML5 Validation
Browser provides built-in validation:
required
pattern
minlength
type="email"
โ Not enough for security
โ Server must validate againโฟ Accessibility & UX
Always use:
โ <label>
โ Descriptive placeholders
โ Helpful error messagesScreen readers rely on:
โ Labels
โ aria-* attributesโ Critical Security Warnings
NEVER:
๐ซ Trust form data
๐ซ Store raw passwords
๐ซ Use GET for login
๐ซ Skip HTTPSALWAYS:
โ Validate on server
โ Hash passwords
โ Use HTTPSForms are the center of all interactive websites.
โ Collect data
They:
โ Validate it
โ Send it to server
โ Power login, signup, search, payments, uploads