HTML forms are one of the most important parts of web development. Forms allow users to enter and submit information to websites.
Forms are used everywhere on the internet, including:
Login pages
Registration forms
Search bars
Feedback forms
Online shopping websites
Banking websites
Admission forms
In this article, we will learn:
What HTML forms are
Form structure
Form elements
Input types
Labels
Buttons
Dropdown lists
Radio buttons
Checkboxes
Textareas
Form attributes
Form validation
Practical examples
What is an HTML Form?
An HTML form is used to collect user data.
The <form> tag creates a form.
Users can enter:
Text
Passwords
Email
Numbers
Dates
Files
Selections
The data is then submitted to a server for processing.
Basic Structure of a Form
<form>
</form>
All form elements are placed inside the <form> tag.
Simple Form Example
<!DOCTYPE html>
<html>
<head>
<title>Simple Form</title>
</head>
<body>
<h1>Student Registration</h1>
<form>
Name:
<input type="text">
<br><br>
<input type="submit">
</form>
</body>
</html>
Output Structure
Student Registration
Name: [___________]
[Submit]
The <input> Tag
The <input> tag is the most commonly used form element.
Syntax:
<input type="value">
The type attribute defines the kind of input.
Common Input Types
| Input Type | Purpose |
|---|---|
| text | Single-line text |
| password | Hidden password |
| Email input | |
| number | Numeric input |
| radio | Single selection |
| checkbox | Multiple selections |
| submit | Submit button |
| reset | Reset form |
| file | Upload files |
| date | Date picker |
| color | Color picker |
1. Text Input
Used for entering text.
Example:
<input type="text">
Example Program
<form>
Enter Name:
<input type="text">
</form>
2. Password Input
Password text becomes hidden.
Example:
<input type="password">
Example
<form>
Password:
<input type="password">
</form>
3. Email Input
Accepts email addresses.
Example:
<input type="email">
Browsers may validate proper email format.
4. Number Input
Used for numeric values.
Example:
<input type="number">
5. Radio Buttons
Radio buttons allow selecting only one option.
Example:
<input type="radio" name="gender"> Male
<input type="radio" name="gender"> Female
Important:
Same
namegroups radio buttons together.
Example Program
<form>
Gender:
<input type="radio" name="gender">
Male
<input type="radio" name="gender">
Female
</form>
6. Checkboxes
Checkboxes allow multiple selections.
Example:
<input type="checkbox"> Cricket
<input type="checkbox"> Football
Example Program
<form>
Hobbies:
<input type="checkbox"> Music
<input type="checkbox"> Reading
<input type="checkbox"> Gaming
</form>
7. Submit Button
Used to submit form data.
Example:
<input type="submit">
8. Reset Button
Resets all form values.
Example:
<input type="reset">
9. File Upload
Allows users to upload files.
Example:
<input type="file">
10. Date Input
Displays a date picker.
Example:
<input type="date">
11. Color Picker
Allows selecting colors.
Example:
<input type="color">
Placeholder Attribute
Displays temporary hint text.
Example:
<input type="text"
placeholder="Enter your name">
Value Attribute
Sets default input value.
Example:
<input type="text"
value="Rahul">
Readonly Attribute
Prevents editing.
Example:
<input type="text"
value="India"
readonly>
Disabled Attribute
Disables input field.
Example:
<input type="text" disabled>
Required Attribute
Makes input mandatory.
Example:
<input type="text" required>
Labels in Forms
The <label> tag improves accessibility.
Example:
<label>Name:</label>
<input type="text">
Label with for Attribute
<label for="username">
Username
</label>
<input type="text" id="username">
Textarea
Used for multi-line input.
Example:
<textarea rows="5" cols="30">
</textarea>
Example
<form>
Address:
<br>
<textarea rows="4"
cols="40"></textarea>
</form>
Dropdown List
Created using:
<select><option>
Example
<select>
<option>India</option>
<option>USA</option>
<option>Japan</option>
</select>
Complete Dropdown Example
<form>
Select Course:
<select>
<option>BCA</option>
<option>BBA</option>
<option>BTech</option>
</select>
</form>
Form Buttons
HTML supports buttons using:
<button><input type="button">
Example:
<button>Click Me</button>
Form Attributes
Important form attributes:
| Attribute | Purpose |
|---|---|
| action | Form submission URL |
| method | HTTP method |
| autocomplete | Enables suggestions |
| target | Where response opens |
Action Attribute
Defines where form data goes.
Example:
<form action="submit.php">
Method Attribute
Defines submission method.
| Method | Purpose |
|---|---|
| GET | Visible data in URL |
| POST | Secure data transfer |
Example
<form method="POST">
GET vs POST
| Feature | GET | POST |
|---|---|---|
| Data Visible | Yes | No |
| Security | Less | More |
| URL Length Limit | Yes | No |
Autocomplete Attribute
Example:
<form autocomplete="on">
Browser suggests previous values.
Complete Registration Form
<!DOCTYPE html>
<html>
<head>
<title>Registration Form</title>
</head>
<body>
<h1>Student Registration Form</h1>
<form>
Name:
<input type="text"
placeholder="Enter Name"
required>
<br><br>
Password:
<input type="password">
<br><br>
Email:
<input type="email">
<br><br>
Gender:
<input type="radio"
name="gender"> Male
<input type="radio"
name="gender"> Female
<br><br>
Hobbies:
<input type="checkbox"> Music
<input type="checkbox"> Sports
<br><br>
Course:
<select>
<option>BCA</option>
<option>BBA</option>
<option>BTech</option>
</select>
<br><br>
Address:
<br>
<textarea rows="5"
cols="40"></textarea>
<br><br>
<input type="submit">
<input type="reset">
</form>
</body>
</html>
Output Structure
Student Registration Form
Name: [___________]
Password: [___________]
Email: [___________]
Gender:
( ) Male
( ) Female
Hobbies:
[ ] Music
[ ] Sports
Course:
[BCA ▼]
Address:
[_______________]
[Submit] [Reset]
HTML5 Form Features
HTML5 introduced advanced inputs:
Email validation
Date picker
Range slider
Color picker
URL input
Modern browsers support these features automatically.
Form Validation
Validation ensures proper user input.
Example:
<input type="email" required>
Browser checks:
Empty field
Invalid email format
Advantages of HTML Forms
Collect user information
Enable user interaction
Support registration and login
Essential for web applications
Easy to use
Real-Life Uses of Forms
Forms are used in:
Online shopping websites
Banking systems
Social media platforms
Admission portals
Job applications
Platforms like Facebook, Google, and Amazon use forms extensively for login, registration, and user interaction.
Common Mistakes Beginners Make
1. Forgetting Name Attribute
Wrong:
<input type="text">
Better:
<input type="text" name="username">
2. Missing Labels
Forms become difficult to understand without labels.
3. Using GET for Passwords
Never send sensitive data using GET.
4. Forgetting Required Validation
Users may submit incomplete forms.
Conclusion
HTML forms are the foundation of interactive websites. They allow users to send data, register accounts, login, search, and communicate with websites.
Comments
Post a Comment