Explain it simply
Explain it simply
A contact form is a website's mailbox slot. The visitor fills in a slip of paper with their name, email, the service they want and their message, then drops it through the slot. The form's whole job is to lay that slip out clearly and push it through the slot.
Opening the slip and getting it into your inbox is the job of a program running on a server. HTML cannot send an email by itself. In this project you build the form side completely and then look at exactly what data arrives on the server side.
Problem → Why → Solution
The problem: a form that looks fine and sends nothing useful
Open the form below in a browser and it looks normal. But none of its fields has a name attribute, so when someone presses Send, the request that reaches the server carries no data at all. Placeholders stand in for labels, so the hint text disappears the moment you start typing, and screen reader users never hear what each box is for.
Finally, the action is a mailto: link. That does not send an email; it only asks the visitor's computer to open a mail app. On an office PC or a phone browser without a configured mail app, nothing happens and the customer's message is simply lost.
<form action="mailto:hello@example.com">
<input type="text" placeholder="Your name">
<input type="text" placeholder="Your email">
<textarea placeholder="Message"></textarea>
<div class="btn">Send</div>
</form>Why is there no error?
Browsers are forgiving with HTML. A field without a name is silently left out of the submission, with no warning. A div styled to look like a button renders fine but cannot be activated with the keyboard. That is why you test a contact form by asking what data it sends and where, not by how it looks.
Core concept: a form is a data contract
Think of a form as a contract between the visitor and the server. On the visitor's side, every field must say clearly what it is asking for. On the server's side, the code must know in advance which URL the data arrives at and under which keys. Build the contact form by making four decisions.
1. Ask only for what you need
To reply you need a name and an email, to route the request you need the service, and then the message. Every extra field makes more people abandon the form and adds personal data you must store and protect. This principle is called data minimization.
2. Pair every field with a label
The label's for value must equal the input's id. With that link in place, clicking the label text puts the cursor in the field, and a screen reader announces something like "Full name, edit text" when the field receives focus.
3. Choose the name and the type
The id links things inside the page; the name is the key sent to the server. type="email" shows a keyboard with @ on phones and blocks an incomplete address such as "aye@".
4. Decide where and how it is sent
action="/contact" is the URL that receives the data. method="post" puts the data in the request body instead of the URL. Use post for contact forms so messages never end up in browser history or server access logs.
| Attribute | Its job |
|---|---|
| id + label for | Connects a label to its field inside the page |
| name | Becomes the key in the submitted data |
| required / type | Lets the browser block an incomplete submit |
- Labels are the questions on the slip
- Names are the keys for each answer
- action/method pick the mailbox
- The server checks it and sends mail
The browser's job ends at the mailbox. Sending the email, filtering spam and storing the message all happen on the server.
General pattern
<form action="/endpoint-on-your-server" method="post">
<label for="field-id">Visible question</label>
<input id="field-id" name="fieldKey" type="text" required>
<!-- one label + control per piece of data you really need -->
<button type="submit">Clear action verb</button>
</form>The form says where and how to send. Every field needs a label linked by for/id, a name that becomes the server key, and a fitting type. Submit with a real button whose type is submit, never a div.
Project code: a service contact form
This form belongs to a freelancer who offers web design, AI automation and video editing. All four fields are required, and the first option in the service dropdown is a "Choose one" placeholder with an empty value. Save it in a file, open it in a browser, and press Send message without filling anything in.
<form action="/contact" method="post">
<label for="full-name">Full name</label>
<input id="full-name" name="fullName" type="text" required>
<label for="email">Email</label>
<input id="email" name="email" type="email" required>
<label for="service">Service</label>
<select id="service" name="service" required>
<option value="">Choose one</option>
<option value="web">Web Design</option>
<option value="automation">AI Automation</option>
<option value="video">Video Editing</option>
</select>
<label for="message">Message</label>
<textarea id="message" name="message" rows="5" required></textarea>
<button type="submit">Send message</button>
</form>Code breakdown
- Where and how
<form action="/contact" method="post">On submit, the browser sends a POST request to /contact on this page's domain. If no server code handles /contact yet, you get a 404 or 405 error page. That means the server side is missing, not that the form is wrong.
- Label linked by id
<input id="full-name" name="fullName" type="text" required>id="full-name" is what label for="full-name" points at. name="fullName" is the key that appears in the request body, as in fullName=Aye+Aye. The two do not have to match.
- Built-in email check
<input id="email" name="email" type="email" required>type="email" rejects a value with no @ or nothing after the @. It still accepts "a@b" as valid, and it never checks that the address actually exists.
- Placeholder option
<option value="">Choose one</option>Because the select is required and the first option has an empty value, the form will not submit until the visitor picks a real service. The browser shows a message such as "Please select an item in the list."
- Multi-line message
<textarea id="message" name="message" rows="5" required></textarea>A textarea accepts many lines. rows="5" only sets its visible height; it does not limit the length. Keep nothing between the opening and closing tags, because any text there becomes the default value.
- Real submit button
<button type="submit">Send message</button>Because it is a real button, Tab reaches it and Enter or Space activates it. Pressing Enter inside a text field submits the form too.
With no CSS, the labels and controls (Full name, Email, a Service dropdown showing "Choose one", the Message box) and the "Send message" button flow side by side like words in a line, wrapping when space runs out. Pressing the button with everything empty shows a bubble such as "Please fill out this field." on Full name and sends nothing (the exact wording depends on the browser and its language).- Browser checks required and type
- Encodes name=value pairs into a body
- Sends POST /contact to the server
- Server re-checks, then sends email
Field incomplete
Bubble message, nothing sent
All valid
POST /contact with form body
What actually reaches the server
When a visitor called Aye Aye fills in the form and sends it, the browser sends a request like the one below. The body joins name=value pairs with &, turns spaces into + and encodes @ as %40. This format is application/x-www-form-urlencoded, and every server framework can parse it.
POST request produced by the form
---------------------------------
POST /contact HTTP/1.1
Host: your-site.example
Content-Type: application/x-www-form-urlencoded
fullName=Aye+Aye&email=aye%40example.com&service=web&message=Hi...
|
v
server code: validate again -> send via email service -> store
|
v
303 redirect to /contact/thanks (refresh will not resend)The server code (Node, PHP, Python, anything) checks the fields again and sends the mail to your inbox through an email service or an SMTP server. Then it redirects to a thank-you page. This pattern is called Post/Redirect/Get, and it stops a refresh from sending the message twice. A static site with no server of its own points action at a hosted form service's URL, and that service sends the email instead.
Never send the email from the browser
Calling an email API directly from JavaScript in the page means shipping the API key inside the page. Anyone can read the page source, take that key and send spam through your account. Keep every secret on the server.
In real projects
In real projects
On a business website the contact form is the main path for new customers. Before launch, agencies always run four tests: submit it empty, submit a broken email, complete it with the keyboard only, and submit while the server is down. When the server fails, it should show the form again with the visitor's values still filled in, so a long message is never lost.
Production forms add attributes such as autocomplete="name" and autocomplete="email" so the browser can fill in details it has saved. To fight spam bots, the server adds rate limiting, a hidden honeypot field or a CAPTCHA. A short privacy note next to the form, saying what the data will be used for, builds trust, and in some countries the law requires it.
Common mistakes
Common mistakes
Treating browser validation as security
required and type="email" exist to help honest visitors. Deleting required in DevTools, or sending a POST straight to /contact with curl, skips every one of these checks. The server must re-check each field and escape the message before it ever shows it inside HTML.
Adding an id but forgetting name
If you add an id for the label but no name, the field looks right, accepts input and even validates, yet it never appears in the submitted data. When the server keeps receiving an empty email, check the name attribute first.
Using placeholder instead of a label
Placeholder text vanishes as soon as you type, so when you review a filled form you can no longer tell what each field was. It is usually low-contrast, and assistive technology does not treat it as reliably as a label. Keep placeholders for examples such as "e.g. aye@example.com".
Mini challenge
Upgrade the form for production
Rewrite the project form with autocomplete, an optional phone field, length limits and hint text. Keep the original four name values so the server code does not have to change.
- Add autocomplete="name" and autocomplete="email" to the name and email fields
- Add a type="tel" phone field without required, and mark its label (optional)
- Give the message minlength="10" and maxlength="1000" and link a hint with aria-describedby
- Keep action="/contact" method="post" and a type="submit" button
<form action="/contact" method="post">
<!-- TODO: Full name (name="fullName"), required, autocomplete="name" -->
<!-- TODO: Email (name="email"), type="email", required, autocomplete="email" -->
<!-- TODO: Phone (optional): type="tel", name="phone", autocomplete="tel" -->
<!-- TODO: Service select (name="service") with an empty-value first option -->
<!-- TODO: Message textarea: minlength 10, maxlength 1000 -->
<!-- TODO: hint paragraph with an id, linked via aria-describedby -->
<!-- TODO: submit button -->
</form>Expected: Submitting it empty is blocked. With the phone left blank and the other fields filled correctly, it submits. Typing fewer than 10 characters into the message triggers a message such as "Please lengthen this text".
Show hint
The value of aria-describedby is the id of the hint paragraph. Leave required off the phone field; otherwise visitors without a phone number cannot send the form at all.
Show solution
<form action="/contact" method="post">
<p>
<label for="full-name">Full name</label>
<input id="full-name" name="fullName" type="text" autocomplete="name" required>
</p>
<p>
<label for="email">Email</label>
<input id="email" name="email" type="email" autocomplete="email" required>
</p>
<p>
<label for="phone">Phone (optional)</label>
<input id="phone" name="phone" type="tel" autocomplete="tel">
</p>
<p>
<label for="service">Service</label>
<select id="service" name="service" required>
<option value="">Choose one</option>
<option value="web">Web Design</option>
<option value="automation">AI Automation</option>
<option value="video">Video Editing</option>
</select>
</p>
<p>
<label for="message">Message</label>
<textarea id="message" name="message" rows="5" minlength="10" maxlength="1000"
aria-describedby="message-hint" required></textarea>
</p>
<p id="message-hint">Tell us about your project in 10 to 1000 characters.</p>
<button type="submit">Send message</button>
</form>Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- A form only delivers data to its action URL; sending the email is the server code's job
- label for/id connects things in the page, while name is the key the server reads
- Use method="post" for contact forms and avoid a mailto: action
- required, type="email" and an empty-value option help visitors, but the server must check again
HTML Living Standard — Forms — WHATWG