HTML Β· Chapter 35 of 45

HTML Forms

The <form> element collects user input and sends it to a server for processing, wrapping input fields, buttons, and other controls together.

The action attribute specifies where to send the data, and the method attribute (GET or POST) specifies how. Forms are essential for logins, searches, surveys, and virtually any interactive site feature.

Syntax
<form action="/submit" method="post">...</form>

action and method

action defines the URL that receives submitted data. method="get" appends data to the URL (visible, for searches); method="post" sends data in the request body (better for sensitive or large data).

Form submission

A <button type="submit"> or <input type="submit"> triggers form submission, gathering all named fields' values and sending them per the action/method.

Example 1 (html)
<form action="/search" method="get">
  <input type="text" name="q">
  <button type="submit">Search</button>
</form>
Output
(submits to /search?q=value)

GET appends form field values as URL query parameters.

Example 2 (html)
<form action="/login" method="post">
  <input type="text" name="username">
  <input type="password" name="password">
  <button type="submit">Log In</button>
</form>
Output
(submits username/password securely in the request body)

POST hides submitted data from the URL, more suitable for sensitive fields.

Key points

  • <form> wraps input controls that collect user data.
  • action specifies the URL to submit data to.
  • method (GET or POST) specifies how data is sent.
  • Every meaningful field needs a name attribute to be submitted.
πŸ’‘ Note: Never rely solely on client-side validation β€” always validate form data on the server too.

πŸ“ Quick Quiz

1. Which attribute specifies where form data is sent?

2. Which method appends data visibly to the URL?

3. What attribute must an input have to be included in submitted data?