HTML Forms
Learn to create interactive web forms with HTML. Master form elements, validation, and user input handling for modern web applications.
How do you create a form in HTML?
Wrap the fields in a <form> with an action (where to send the data) and a method (get or post). Give each <input>, <select> or <textarea> a name and a matching <label>, and add a <button type="submit">. The browser then validates required fields and sends the named values when the form is submitted.
<form action="/subscribe" method="post">
<label for="email">Email</label>
<input id="email" name="email" type="email" autocomplete="email" required>
<label for="msg">Message</label>
<textarea id="msg" name="message" rows="4"></textarea>
<button type="submit">Send</button>
</form>What are HTML Forms?
HTML forms are interactive elements that allow users to input and submit data to web servers. They are essential for user registration, login, contact forms, surveys, and any application requiring user input.
Form Elements:
- •
<form>- Container for form controls - •
<input>- Various input types - •
<label>- Accessible labels - •
<button>- Submit and reset buttons - •
<select>- Dropdown lists - •
<textarea>- Multi-line text
Common Uses:
- • User registration and login
- • Contact and feedback forms
- • Search and filtering
- • Payment and checkout
- • Surveys and questionnaires
- • Settings and preferences
Try It Yourself
Create a basic contact form with essential form elements:
Code Editor
<form onsubmit="event.preventDefault(); out.textContent = 'Sent: ' + new URLSearchParams(new FormData(this))">
<label for="name">Name</label>
<input id="name" name="name" required>
<label for="email">Email</label>
<input id="email" name="email" type="email" required>
<label for="level">Level</label>
<select id="level" name="level">
<option>Beginner</option>
<option>Intermediate</option>
</select>
<label><input type="checkbox" name="news"> Send me tips</label>
<button type="submit">Sign up</button>
</form>
<p id="out"></p>
<style>
form { display: grid; gap: 6px; max-width: 280px; font-family: sans-serif; }
button { margin-top: 6px; }
</style>Preview
What to Notice:
- • Form has action and method attributes
- • Labels are properly associated with inputs
- • Required fields are marked with asterisks
- • Different input types for different data
- • CSS styling makes the form attractive
Form Structure & Attributes
The <form> Element
Basic Structure:
<form action="/submit" method="POST">
<!-- Form controls go here -->
<input type="text" name="username">
<button type="submit">Submit</button>
</form>URL where form data is sent
HTTP method (GET or POST)
Text Inputs:
type="text" - Basic text inputtype="email" - Email validationtype="password" - Hidden texttype="tel" - Phone numbersSpecial Inputs:
type="date" - Date pickertype="number" - Numeric inputtype="file" - File uploadtype="checkbox" - CheckboxesForm Validation
HTML5 provides built-in validation attributes to ensure data quality:
Validation Attributes:
Example Validation:
<!-- Required email field -->
<input type="email"
name="email"
required
placeholder="Enter your email">
<!-- Password with min length -->
<input type="password"
name="password"
required
minlength="8"
placeholder="Min 8 characters">Form Best Practices
Do's:
- • Use semantic HTML elements
- • Add labels to all form controls
- • Implement client-side validation
- • Provide clear error messages
- • Make forms accessible
- • Use appropriate input types
Don'ts:
- • Missing form labels
- • No validation feedback
- • Too many required fields
- • Poor mobile experience
- • Confusing form layout
- • Unclear button purposes
Accessibility Tips:
- • Associate labels with form controls
- • Use fieldsets for related inputs
- • Provide helpful placeholder text
- • Ensure keyboard navigation works
- • Add ARIA labels when needed
- • Test with screen readers
Quick Reference
Basic Form Structure:
<form action="/submit" method="POST">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required>
<button type="submit">Submit</button>
</form>Common Attributes:
name="fieldName" <!-- Field identifier -->
id="uniqueId" <!-- For label association -->
required <!-- Makes field mandatory -->
placeholder="text" <!-- Hint text -->
value="default" <!-- Default value -->
readonly <!-- Cannot be edited -->
disabled <!-- Cannot be used -->HTML Forms — step by step
Create a form element
The form element wraps all inputs. Its action attribute specifies where data is sent and method specifies GET (query string) or POST (request body).
🎯 Quick Check
Which form method sends data as part of the request body, not the URL?
HTML Forms: summary
Key points
- Only fields with a
nameattribute are submitted. method="get"puts data in the URL, which suits searches.postputs it in the request body, which suits anything that changes data or is sensitive.autocompletevalues likeemail,nameandpostal-codelet browsers fill forms automatically.- Group related fields with
<fieldset>and<legend>.
Common mistakes
- Using a placeholder instead of a
<label>: the placeholder disappears when typing and isn't a reliable accessible name. - Relying only on browser validation. Always validate on the server too.
Frequently asked questions
›What is the difference between GET and POST in forms?
GET appends the data to the URL, so it's visible, bookmarkable and limited in length; use it for searches and filters. POST sends the data in the request body; use it for logins, sign-ups and anything that changes data.
›Can I submit a form without a page reload?
Yes. Listen for the submit event in JavaScript, call event.preventDefault(), and send the data with fetch(), often using new FormData(form).