HTML Forms
HTML forms are used to collect information and data from users. They allow visitors to enter text, select options, upload files, and submit information to a server.
HTML forms are commonly used for:
- User registration and login pages.
- Contact forms and feedback forms.
- Online surveys and quizzes.
- Search boxes.
- Online shopping and checkout pages.
- File upload forms.
1. The HTML <form> Element
The <form> element defines an HTML form. It contains different input fields and controls that allow users to enter and submit information.
Example:
<form>
<label for="username">Username:</label>
<input type="text" id="username" name="username">
<button type="submit">Submit</button>
</form>
2. HTML Form Attributes
The <form> element supports several attributes that control how form data is submitted.
| Attribute | Description |
|---|---|
| action | Specifies where the form data is sent. |
| method | Specifies the HTTP method, usually GET or POST. |
| target | Specifies where to display the response. |
| enctype | Specifies how form data is encoded when submitted. |
| autocomplete | Controls automatic form completion. |
| novalidate | Disables built-in browser form validation. |
3. HTML Input Element
The <input> element is one of the most important form elements. It allows users to enter different types of information depending on its type attribute.
Example:
<form>
<input type="text" placeholder="Enter your name">
<input type="email" placeholder="Enter your email">
<input type="password" placeholder="Enter password">
<input type="submit" value="Register">
</form>
4. HTML Input Types
HTML provides different input types for collecting specific kinds of information.
| Input Type | Purpose |
|---|---|
| text | Single-line text input. |
| password | Password input with hidden characters. |
| Email address input. | |
| number | Numeric input. |
| tel | Telephone number input. |
| url | Website URL input. |
| date | Date selection. |
| time | Time selection. |
| datetime-local | Date and time selection. |
| month | Month and year selection. |
| week | Week and year selection. |
| checkbox | Allows multiple selections. |
| radio | Allows one selection from a group. |
| file | File upload input. |
| color | Color selection. |
| range | Slider for selecting a value. |
| search | Search input field. |
| hidden | Stores a value not displayed to the user. |
| submit | Submits the form. |
| reset | Resets form fields to their initial values. |
| button | Creates a clickable button. |
5. HTML Form Labels
The <label> element defines a label for a form control. Use the for attribute to connect a label to an input element with the same id.
Example:
<form>
<label for="name">Full Name:</label>
<input type="text" id="name" name="name">
</form>
6. HTML Radio Buttons
Radio buttons allow users to select only one option from a group. Radio buttons with the same name attribute belong to the same group.
Example:
<form>
<p>Select your gender:</p>
<input type="radio" id="male" name="gender" value="male">
<label for="male">Male</label>
<input type="radio" id="female" name="gender" value="female">
<label for="female">Female</label>
</form>
7. HTML Checkboxes
Checkboxes allow users to select one or more options from a group.
Example:
<form>
<p>Choose your skills:</p>
<input type="checkbox" id="html" name="skills" value="HTML">
<label for="html">HTML</label>
<input type="checkbox" id="css" name="skills" value="CSS">
<label for="css">CSS</label>
<input type="checkbox" id="js" name="skills" value="JavaScript">
<label for="js">JavaScript</label>
</form>
8. HTML Dropdown Lists
The <select> element creates a dropdown list. Each available option is defined using the <option> element.
Example:
<form>
<label for="country">Choose your country:</label>
<select id="country" name="country">
<option value="india">India</option>
<option value="usa">USA</option>
<option value="uk">United Kingdom</option>
</select>
</form>
9. HTML Textarea
The <textarea> element allows users to enter multiple lines of text. It is commonly used for messages, comments, and feedback.
Example:
<form>
<label for="message">Your Message:</label>
<textarea id="message" name="message"
rows="5" cols="30"></textarea>
</form>
10. HTML Fieldset and Legend
The <fieldset> element groups related form controls together. The <legend> element provides a caption for that group.
Example:
<form>
<fieldset>
<legend>Personal Information</legend>
<label for="fname">Name:</label>
<input type="text" id="fname" name="fname">
<label for="email">Email:</label>
<input type="email" id="email" name="email">
</fieldset>
</form>
11. HTML Form Validation
HTML provides built-in validation attributes that help ensure users enter the required information before submitting a form.
| Attribute | Description |
|---|---|
| required | Makes a field mandatory. |
| minlength | Sets the minimum text length. |
| maxlength | Sets the maximum text length. |
| min | Sets the minimum allowed value. |
| max | Sets the maximum allowed value. |
| pattern | Defines a regular expression that the value must match. |
| step | Defines valid intervals for numeric inputs. |
Example:
<form>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required>
<label for="age">Age:</label>
<input type="number" id="age" name="age"
min="18" max="60" required>
<button type="submit">Submit</button>
</form>
12. HTML GET and POST Methods
The method attribute specifies how the browser sends form data to the server.
GET Method:
- Appends form data to the URL as query parameters.
- Useful for search forms and filters.
- Data is visible in the URL.
<form action="search.php" method="GET">
<input type="search" name="query">
<button type="submit">Search</button>
</form>
POST Method:
- Sends form data in the HTTP request body.
- Commonly used for registration and login forms.
- Does not display submitted fields in the URL.
<form action="register.php" method="POST">
<input type="text" name="username">
<input type="password" name="password">
<button type="submit">Register</button>
</form>
13. HTML File Upload
The type="file" input allows users to select files from their devices. To upload files using a form, use the POST method and enctype="multipart/form-data".
Example:
<form action="upload.php" method="POST"
enctype="multipart/form-data">
<label for="photo">Choose a file:</label>
<input type="file" id="photo" name="photo">
<button type="submit">Upload</button>
</form>
14. HTML Buttons
HTML provides different button types for form operations.
| Button Type | Purpose |
|---|---|
| submit | Submits the form. |
| reset | Resets form controls to their initial values. |
| button | Creates a general-purpose clickable button. |
Example:
<form>
<input type="text" name="username"
placeholder="Enter username">
<button type="submit">Submit</button>
<button type="reset">Reset</button>
<button type="button">Click Me</button>
</form>
15. Complete HTML Registration Form
Here is a complete registration form that combines different HTML form elements and CSS styling.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Registration Form</title>
<style>
body {
font-family: Arial, sans-serif;
background: #f1f5f9;
padding: 20px;
}
form {
max-width: 400px;
margin: auto;
padding: 25px;
background: white;
border-radius: 10px;
}
input, select, textarea {
width: 100%;
padding: 10px;
margin: 8px 0 15px;
box-sizing: border-box;
}
button {
padding: 12px 20px;
background: #2563eb;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
}
</style>
</head>
<body>
<form action="#" method="POST">
<h2>Registration Form</h2>
<label for="name">Full Name:</label>
<input type="text" id="name" name="name" required>
<label for="email">Email Address:</label>
<input type="email" id="email" name="email" required>
<label for="password">Password:</label>
<input type="password" id="password"
name="password" required>
<label for="course">Select Course:</label>
<select id="course" name="course">
<option value="html">HTML</option>
<option value="css">CSS</option>
<option value="javascript">JavaScript</option>
</select>
<button type="submit">Register</button>
</form>
</body>
</html>
Conclusion
HTML forms are an essential part of web development. They allow websites to collect user information through input fields, radio buttons, checkboxes, dropdown menus, textareas, and file uploads. Understanding HTML forms is an important step toward building interactive websites and full-stack web applications.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HTML Forms Practice</title>
<style>
body {
font-family: Arial, sans-serif;
background: #f1f5f9;
padding: 20px;
}
.container {
max-width: 420px;
margin: auto;
padding: 25px;
background: white;
border-radius: 12px;
}
h2 {
text-align: center;
color: #2563eb;
}
label {
display: block;
margin-top: 12px;
font-weight: bold;
}
input, select, textarea {
width: 100%;
padding: 10px;
margin-top: 6px;
box-sizing: border-box;
border: 1px solid #cbd5e1;
border-radius: 6px;
}
.choice {
display: flex;
align-items: center;
gap: 8px;
}
.choice input {
width: auto;
}
button {
width: 100%;
padding: 12px;
margin-top: 15px;
background: #2563eb;
color: white;
border: none;
border-radius: 6px;
cursor: pointer;
}
</style>
</head>
<body>
<div class="container">
<h2>Student Registration</h2>
<form id="registrationForm">
<label for="name">Full Name:</label>
<input type="text" id="name"
placeholder="Enter your name" required>
<label for="email">Email:</label>
<input type="email" id="email"
placeholder="Enter your email" required>
<label for="password">Password:</label>
<input type="password" id="password"
minlength="8"
placeholder="Minimum 8 characters" required>
<label for="phone">Phone Number:</label>
<input type="tel" id="phone"
placeholder="Enter phone number">
<label for="course">Select Course:</label>
<select id="course" required>
<option value="">Choose Course</option>
<option>HTML</option>
<option>CSS</option>
<option>JavaScript</option>
<option>Python</option>
</select>
<label>Gender:</label>
<div class="choice">
<input type="radio" name="gender" value="Male" required>
<span>Male</span>
</div>
<div class="choice">
<input type="radio" name="gender" value="Female">
<span>Female</span>
</div>
<label for="message">About Yourself:</label>
<textarea id="message" rows="4"
placeholder="Write something..."></textarea>
<div class="choice" style="margin-top:15px;">
<input type="checkbox" id="terms" required>
<span>Accept Terms and Conditions</span>
</div>
<button type="submit">Register</button>
<button type="reset" style="background:#64748b;">
Reset
</button>
</form>
<p id="result" style="text-align:center;"></p>
</div>
<script>
document.getElementById("registrationForm")
.addEventListener("submit", function(event) {
event.preventDefault();
const name = document.getElementById("name").value;
document.getElementById("result").textContent =
"Welcome, " + name + "! Form submitted successfully.";
});
</script>
</body>
</html>