← Learning Hub
Beginner

Forms

Forms collect input from visitors: names, email addresses, choices and more. <input> is the most common form element, and its type attribute decides what kind of input it is.

Every input should have a <label>. Linking the label to the input with matching for and id values lets visitors tap the label to focus the box.

Forms are how websites talk back: sign-up pages, search boxes, checkout pages, contact forms, feedback surveys. When a visitor submits a form, the browser collects the values and sends them to a server, which saves them or replies.

Choosing the right type makes forms much easier on phones: email shows a keyboard with @, tel a number pad, date a calendar. Other useful elements are <textarea> for long text, <select> for a drop-down, and radio buttons and checkboxes for choices.

Example
<!DOCTYPE html>
<html>
  <body>
    <form onsubmit="event.preventDefault(); document.getElementById('msg').textContent = 'Thanks, ' + document.getElementById('name').value + '!'">
      <label for="name">Your name</label><br>
      <input id="name" type="text" required><br><br>

      <label for="city">City</label><br>
      <select id="city">
        <option>Delhi</option>
        <option>Patna</option>
        <option>Mumbai</option>
      </select><br><br>

      <input id="news" type="checkbox">
      <label for="news">Send me offers</label><br><br>

      <button type="submit">Submit</button>
    </form>
    <p id="msg"></p>
  </body>
</html>
▶ Try it yourself
  • type="email", type="number" and type="date" give mobile users the right keyboard or picker.
  • required stops the form being sent while the box is empty.
  • Real forms send data to a server, which must check it again; never trust browser checks alone.
Key points
  • <form> collects input; <input type="..."> decides the kind of input.
  • Always connect a <label> to each input with for and id.
  • Browser checks like required help, but the server must check again.

📝 Test yourself

1. Which attribute decides what kind of input box appears?
2. Why link a label to an input?
3. Fill in the attribute that makes a field mandatory:
<input type="text" >