HTML Forms
Collect user input with accessible, semantic HTML forms
Introduction to HTML Forms
What is an HTML Form?
An HTML form collects user input and sends it to a server for processing. Forms are built with the <form> element and various input controls such as <input>, <textarea>, <select>, and <button>. Every login page, search box, checkout page, or feedback form you see on the web is powered by HTML forms.
Basic Form Structure
Example: Contact Form
HTML Code:
<form action="/submit" method="post">
<label for="name">Name:</label>
<input type="text" id="name" name="name">
<label for="email">Email:</label>
<input type="email" id="email" name="email">
<button type="submit">Submit</button>
</form>
Common Input Types
The <input> element supports many different type values. Some of the most common are:
type="text"– single-line text input.type="email"– email address input with basic validation.type="password"– masked password field.type="checkbox"– multiple-choice options where more than one can be selected.type="radio"– a single choice from a named group of options.type="submit"– submit button to send the form.
Labels & Accessibility
Every input should be associated with a <label> to improve usability and accessibility.
- Use
foron labels to match the inputid. - Ensure form controls have clear, descriptive labels.
Accessible Label Example:
<label for="email">Email address</label>
<input type="email" id="email" name="email" required>
Important Form Attributes
action- URL where form data is submitted.method- HTTP method, usuallygetorpost.autocomplete- helps browser fill known values.enctype- required for file uploads (multipart/form-data).
Example:
<form action="/register" method="post" autocomplete="on">
...
</form>
<form action="/upload" method="post" enctype="multipart/form-data">
<input type="file" name="resume">
</form>