HTML forms are the primary way websites collect information from users—contact messages, logins, surveys, and search queries all start with a <form> element wrapping interactive controls.
This tutorial covers form structure, attributes, validation, submission, JavaScript handling with FormData, common mistakes, and six hands-on examples you can edit in the Try It editor.
What You’ll Learn
01
form
Container.
02
label
Accessible.
03
action
Send data.
04
validate
required.
05
submit
POST / GET.
06
FormData
Read in JS.
Fundamentals
What Is an HTML Form?
An HTML form is a section of a document that contains controls where users enter or choose data. When the form is submitted, the browser gathers values from named fields and sends them to a destination URL or passes them to JavaScript.
Forms connect the front end to server-side scripts, APIs, or client-side logic. Every control that should contribute data needs a name attribute, and accessible forms pair each input with a <label>. See the form tag reference for the full attribute list.
💡
Beginner Tip
All inputs, textareas, selects, and submit buttons must be inside the <form> element. Closing the form too early leaves controls outside and breaks submission.
Structure
Form Elements
A typical form combines several HTML elements, each with a specific role:
<form> — wraps all controls and defines where data goes.
<label> — describes a field; link it with for matching the input’s id.
Use type="button" for buttons that should not submit (e.g. “Cancel” or “Add row”). Only type="submit" (the default for <button> inside a form) triggers submission.
JavaScript
Handling Form Data
JavaScript can intercept submission, read values without a page reload, and send data to an API. Use FormData to collect all named fields:
js
const form = document.getElementById("contactForm");
form.addEventListener("submit", function (event) {
event.preventDefault();
const data = new FormData(form);
console.log("Name:", data.get("name"));
console.log("Email:", data.get("email"));
// Send to server with fetch(data) or display a success message
});
event.preventDefault() — stops the browser from navigating away.
new FormData(form) — reads all named controls in one object.
data.get("name") — retrieves a single field value by name.
Caution
Common Pitfalls
Missing name — fields without name are excluded from submission data.
Closing form too early — inputs or buttons outside <form> are not submitted.
No labels — placeholders are not substitutes; use <label for="id">.
Client-only validation — always validate again on the server for security.
Wrong enctype — file uploads need multipart/form-data.
Multiple submit buttons — only the clicked button’s name/value is sent; plan accordingly.
Hands-On
Examples Gallery
Six examples from a basic contact form to a complete page with JavaScript handling. Each includes View Output and Try It Yourself.
JavaScript intercepts submit, builds FormData, and shows a success message without reloading the page. Open Try It to see the full script.
Pro Tips
Best Practices
✅ Do
Give every submittable control a name attribute
Pair inputs with <label for="id">
Keep all fields and submit buttons inside <form>
Use method="post" for sensitive data
Validate on both client and server
❌ Don’t
Rely on placeholders instead of labels
Close </form> before inputs or buttons
Trust browser validation alone for security
Forget enctype when uploading files
Leave optional fields without clear indication
Wrap Up
Conclusion
HTML forms are the bridge between users and your application. By wrapping controls correctly, setting action and method, validating input, and reading data with FormData, you can build accessible, reliable data collection on any site.
A form is a container element that groups interactive controls—inputs, textareas, selects, and buttons. When submitted, the browser collects named field values and sends them to a server or handles them with JavaScript.
On submit, the name becomes the key in the data sent to the server. Fields without a name are ignored. Always set name on controls you want included in the submission.
GET appends form data to the URL as query parameters—fine for searches. POST sends data in the request body—preferred for logins, registrations, and anything sensitive.
It sets how form data is encoded. Use enctype="multipart/form-data" when uploading files. The default application/x-www-form-urlencoded works for most text fields.
No. Browser validation improves user experience but can be bypassed. Always validate and sanitize data again on the server before storing or using it.
Listen for the submit event, call event.preventDefault() to stop navigation, then use new FormData(form) to read values with data.get("fieldName") or iterate all entries.
Cheat Sheet
⚡ Quick Reference
Element / Attribute
Purpose
<form>
Container for controls
action
Submission URL
method
get or post
enctype
Data encoding (files need multipart)
name (on inputs)
Key sent with form data
<label for>
Accessible field label
required
Mandatory field
FormData
Read values in JavaScript
Summary
Key Takeaways
📝01
name
Required.
Submit
🏷️02
label
Always pair.
a11y
📥03
action
Where sent.
POST
✅04
validate
Client + server.
Security
⚙️05
FormData
JS access.
API
Compatibility
Universal Browser Support
HTML forms work in every browser. HTML5 validation, FormData, and fieldset are supported in all modern browsers. Test file uploads and custom validation messages on mobile Safari and Chrome.
✓ Baseline · Since HTML
HTML forms + FormData
HTML forms work in every browser. HTML5 validation, FormData, and fieldset are supported in all modern browsers. Test file uploads and custom validation messages on mobile Safari and Chrome.
98%Modern browser support
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
HTML forms + FormDataModern browsers
Bottom line: Core form elements are universal; HTML5 validation and FormData cover virtually all users today.
Did you know?
Pressing Enter inside a text input submits the form automatically—the browser activates the first submit button. Use type="button" on non-submit buttons to avoid accidental submissions.