Forms
Forms are how users talk back to your website — logging in, searching, buying, sending a message. This lesson covers every basic building block.
On this page · 5 min · Beginner
On this page
- Learning objectives
- Explanation
- Syntax
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Beginner
- Updated
- Oct 28, 2026
Related tutorials
Learning objectives#
- Create a form with action and method
- Connect every input to a <label>
- Use text inputs, textarea, select, radio buttons and checkboxes
- Group related fields with <fieldset> and <legend>
- Explain the name attribute and GET vs POST
Explanation#
<form action="/signup" method="post"> wraps the fields. action is where the data is sent; method is how.
GET puts the data in the URL (?q=shoes) — good for searches you can bookmark. POST sends it in the request body — use it for logins, payments and anything that changes data.
Each field needs a name. The browser sends name=value pairs; without a name the field is not sent at all.
<label for="id"> connects text to a field. Clicking the label focuses the field, and screen readers read the label aloud.
Radio buttons with the same name form a group where only one can be chosen. Checkboxes are independent on/off choices.
Syntax#
<form action="/submit" method="post">
<label for="x">Label</label>
<input id="x" name="x">
<button type="submit">Send</button>
</form>
Code example: A complete contact form#
<style>
form { font-family: sans-serif; max-width: 320px; }
label, legend { display: block; margin-top: 10px; }
input:not([type=radio]):not([type=checkbox]), select, textarea { width: 100%; }
</style>
<form action="https://httpbin.org/post" method="post">
<label for="name">Full name</label>
<input id="name" name="name" autocomplete="name" required>
<label for="topic">Topic</label>
<select id="topic" name="topic">
<option value="">Choose…</option>
<option value="sales">Sales</option>
<option value="support">Support</option>
</select>
<fieldset>
<legend>Preferred contact</legend>
<label><input type="radio" name="contact" value="email" checked> Email</label>
<label><input type="radio" name="contact" value="phone"> Phone</label>
</fieldset>
<label for="msg">Message</label>
<textarea id="msg" name="message" rows="4"></textarea>
<label><input type="checkbox" name="newsletter" value="yes"> Send me updates</label>
<p><button type="submit">Send message</button></p>
</form>
A contact form with a name field, a topic dropdown, two radio options, a message box, a checkbox and a submit button.
Step-by-step explanation#
- The form posts to httpbin.org, a test service that echoes back what it receives.
- Each <label for> matches an input id.
- <select> creates a dropdown; each <option> has a value that is sent.
- The two radio buttons share name="contact", so only one can be selected. checked makes Email the default.
- <fieldset> and <legend> group the radios under a common question.
- <textarea> is a multi-line text box.
- Wrapping an input inside its <label> is another valid way to link them.
- <button type="submit"> sends the form.
Real-world use case#
Login pages, checkout pages, search bars, feedback forms and job applications are all HTML forms — React and ASP.NET forms render the same elements.
Common mistakes#
An input without name is never sent to the server. This is the #1 “my data is missing” bug.
GET puts values in the URL, browser history and server logs. Always use POST for sensitive data.
If each radio has its own name, the user can select all of them. Give the group one shared name.
Best practices#
- Every field gets a visible <label>
- Use POST for anything private or that changes data
- Add autocomplete hints (name, email, tel) to help browsers fill fields
- Use <button type="submit">, and type="button" for non-submit buttons inside forms
Remember this#
No name = not sent. name is the key, value is the answer.
GET = postcard (everyone sees the URL). POST = sealed envelope.
Radio = pick ONE (same name). Checkbox = pick ANY.
Practice exercise#
Your task: Build a login form that POSTs to /login with a username, a password, a “remember me” checkbox and a submit button. Link all labels.
Need a hint?
Show solution
<form action="/login" method="post">
<label for="user">Username</label>
<input id="user" name="username" autocomplete="username" required>
<label for="pass">Password</label>
<input id="pass" name="password" type="password" autocomplete="current-password" required>
<label><input type="checkbox" name="remember" value="1"> Remember me</label>
<button type="submit">Log in</button>
</form>
Mini quiz#
Which attribute decides the key under which a field’s value is sent?
Want more? Practise all HTML MCQs · HTML interview questions
My notes#
Saved in this browser.