Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / HTML

form — HTML form

The form element associates controls with a form and defines how their data is submitted. It supports native submission and validation; JavaScript can intercept submission.

Version: WHATWG HTML Living Standard; index snapshot date recorded separately

Syntax

<form action="/search" method="get">…</form>

form represents a form whose controls can supply data for processing. Both tags are required; forms must not be nested. Supported controls outside the form can be associated with it through a form attribute referencing its id.

action specifies the destination. method="get" (the default) puts data in the URL query, whereas method="post" puts it in the request body. For file uploads using POST, use enctype="multipart/form-data". POST alone does not provide encryption.

Data keys come from name, not id. Disabled controls and unchecked checkboxes are omitted. Unnamed controls are generally omitted, but an unnamed image submit button (<input type="image">) used to submit the form adds coordinate entries named x and y. Associate labels with fields. Browser validation helps users but does not replace server validation.

Normal submission runs interactive validation before firing the submit event, unless validation is skipped. If validation fails, submission stops and the submit event does not fire. The novalidate attribute skips interactive validation. In scripts, prefer requestSubmit(): unlike submit(), it preserves validation and the submit event.

Examples

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>HTML form example</title>
</head>
<body>
  <form id="search" method="get">
    <label for="query">Query</label>
    <input id="query" name="q" value="html" required>
    <button id="submit-query" type="submit">Submit query</button>
  </form>
  <output id="result" for="query" aria-live="polite"></output>
  <script>
    const form = document.getElementById('search');
    const submitButton = document.getElementById('submit-query');
    const result = document.getElementById('result');

    form.addEventListener('submit', (event) => {
      event.preventDefault();
      const data = new FormData(form);
      result.value = 'q=' + data.get('q');
      console.log(result.value);
    });

    form.requestSubmit(submitButton);
  </script>
</body>
</html>

Result

q=html

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources