Lesson 26 of 36
HTML Tutorial

A Complete Contact Form

Combine labels, input types, select, textarea, validation and fieldsets into one accessible contact form.

3 min readBeginnerWeb FundamentalsHTML Forms

Time to combine everything from this section into one realistic form: the contact page of the Brew and Byte cafe. Visitors can ask a question, book an event space or leave feedback.

Plan before you type#

Good forms start with a list of what you really need. Every extra field lowers the number of people who finish. For our form we need:

  • name (required)
  • email (required), so we can reply
  • phone (optional)
  • topic, chosen from a short list
  • preferred contact method, one choice
  • message (required)
  • consent to be contacted (required)

The finished form#

html
<form action="/contact" method="post">
  <h2>Contact Brew and Byte</h2>
  <p>Fields marked (required) must be completed.</p>

  <div>
    <label for="name">Full name (required)</label>
    <input type="text" id="name" name="name" autocomplete="name" required minlength="2">
  </div>

  <div>
    <label for="email">Email (required)</label>
    <input type="email" id="email" name="email" autocomplete="email" required
           aria-describedby="email-hint">
    <p id="email-hint">We only use this to reply to you.</p>
  </div>

  <div>
    <label for="phone">Phone (optional)</label>
    <input type="tel" id="phone" name="phone" autocomplete="tel">
  </div>

  <div>
    <label for="topic">Topic</label>
    <select id="topic" name="topic" required>
      <option value="">-- Choose a topic --</option>
      <option value="question">General question</option>
      <option value="event">Book the event space</option>
      <option value="feedback">Feedback</option>
    </select>
  </div>

  <fieldset>
    <legend>How should we reply?</legend>
    <label><input type="radio" name="reply_by" value="email" checked> By email</label>
    <label><input type="radio" name="reply_by" value="phone"> By phone</label>
  </fieldset>

  <div>
    <label for="message">Message (required)</label>
    <textarea id="message" name="message" rows="6" required minlength="10" maxlength="1000"></textarea>
  </div>

  <div>
    <label>
      <input type="checkbox" name="consent" value="yes" required>
      I agree to be contacted about this message (required)
    </label>
  </div>

  <button type="submit">Send message</button>
</form>

Walk-through of the decisions#

  • Every control has a label, using for and id or wrapping.
  • Specific input types (email, tel) give the right keyboard and checks.
  • autocomplete tokens let browsers fill known details.
  • The radio group sits in a fieldset with a legend, so its question is announced.
  • Hint text is linked with aria-describedby.
  • Validation attributes catch obvious mistakes immediately.
  • The button text is specific: "Send message", not "Submit".
  • Each row is wrapped in a div purely as a styling hook. Later, CSS can lay them out in a column.

After the form is submitted#

The server receives name and value pairs, validates them again, stores or emails the message, and responds with a thank-you page or a clear error. Always confirm the result to the user, for example "Thanks, we will reply within two days". A form that silently does nothing is frustrating.

Common mistakes#

  • Asking for information you do not need.
  • Splitting a name into many fields when one is enough.
  • Missing name attributes, so nothing is sent.
  • Using a placeholder as the label.
  • No confirmation after sending.

Try it yourself#

Build this form in a file called contact.html and extend it. Add a date field "Preferred event date" that only matters for the event topic, using min set to today. Make sure all labels work by clicking them, and complete the form using only the keyboard. Submit it with method get temporarily and check that each named field appears in the query string.

Recap#

A strong form asks only what it needs, labels every control, uses the most specific input types, groups related choices, validates in the browser and again on the server, and confirms success. You now know every ingredient.

Check your understanding

Lesson quiz

Optional

A short quiz to check what you just learned.

Questions
4
Time limit
5 min
Pass mark
70%
Best score
-