Skip to content
Tutorials Center
My learning

Topics

Lesson 70 of 122 ยท Forms

Form Attributes

Configure the form element itself with action, method, target, enctype for file uploads, autocomplete, novalidate, name and accept-charset.

So far this chapter has looked at the controls inside a form. The <form> element has attributes of its own, and they decide what happens to all that data: where it goes, how it is packaged, and where the response appears. Most forms need only one or two of them, but when you need a file upload or a form that opens its result in a new tab, you need to know which switch to flip.

action: where the data goes

action holds the URL that receives the submission:

HTML
<form action="/newsletter/subscribe" method="post">

It accepts any URL: absolute, root-relative or relative, as explained in File Paths and URLs. If action is missing or empty, the form submits to the URL of the current page.

A GET form with an action that already has a query string will replace that query string with the form data, not add to it. If you need fixed extra values, put them in hidden inputs instead.

method: how the data travels

You met the two main values in Forms:

  • get (the default) puts the data in the URL's query string. Use it for searches and filters.
  • post puts the data in the request body. Use it for anything that changes data.

There is a third value, dialog. A form with method="dialog" inside a <dialog> element closes the dialog instead of sending anything to a server. You will use it in Dialogs and Modals.

Values are case-insensitive, and anything unrecognized falls back to get. HTML forms cannot send PUT, PATCH or DELETE requests directly. Server frameworks work around that with a hidden input, such as Laravel's _method field, while the form itself still uses POST.

enctype: how the data is packaged

When a form is sent with POST, the browser has to encode the pairs into the request body. The enctype attribute picks the format:

enctype Body looks like Use it for
application/x-www-form-urlencoded (default) name=Ana&city=Lima Ordinary forms without files
multipart/form-data Separate parts, divided by a boundary string Any form with type="file"
text/plain name=Ana on separate lines, unescaped Nothing, really (see below)

The default format is compact, but it can only carry text. Files need multipart/form-data, where every field becomes its own part with headers, and a file part carries the file's name, type and raw bytes. Forget it, and the server receives only the file name:

HTML
<form action="/profile/photo" method="post" enctype="multipart/form-data">
  <label for="photo">Profile photo</label>
  <input type="file" id="photo" name="photo" accept="image/png, image/jpeg">
  <button>Upload</button>
</form>

enctype only matters for POST. GET always uses the URL-encoded format, in the URL. text/plain cannot be decoded reliably (a value containing = or a line break becomes ambiguous) and should be avoided.

The browser can show you both real formats. In this example, the script asks it to encode the same data as URL-encoded text and as multipart, using the same machinery a real submission uses:

Comparing encodings

Press Encode. The URL-encoded version is one short line, with & turned into %26 and spaces into +. The multipart version is much longer, with a random boundary line between parts. Choose a small text file and encode again: its contents appear inside the multipart body (shortened to 800 characters here), while the URL-encoded version only has the file name.

target: where the response opens

Like a link's target, the form's target decides where the server's response is shown:

  • _self (the default): the current tab or frame.
  • _blank: a new tab or window.
  • _parent or _top: the parent frame or the whole window, when the form sits inside an iframe.
  • A name: the iframe or window with that name.

Opening a form result in a new tab surprises people as much as it does with links, so keep it for cases like a "print preview" button. Link Targets and Page Jumps discusses the trade-offs in more detail. Forms also accept rel values such as noopener and noreferrer, and _blank targets already imply noopener in modern browsers.

autocomplete on the form

autocomplete="off" on the <form> sets the default for every control inside it, and each control can still override it with its own autocomplete attribute. Browsers treat it as a request rather than a command; they generally keep offering saved passwords and addresses, because blocking password managers makes accounts less secure. The detailed tokens are covered in Input Attributes.

novalidate: turning off built-in validation

The boolean novalidate attribute tells the browser to skip its own validation when the form is submitted. Controls still know whether they are valid; the browser just stops blocking submission and showing its message bubbles:

HTML
<form action="/signup" method="post" novalidate>

Why would you do that? Usually because you are replacing the browser's messages with your own, using JavaScript and the validation API from Form Validation. Keep the required, type and pattern attributes anyway: your script can still read them, and they still help assistive technology.

To skip validation for one button only, use formnovalidate on that button, as shown in Text Areas and Buttons.

name: finding a form in JavaScript

The name attribute on a form is not submitted. It gives scripts a way to find the form through document.forms, which lists every form on the page. The form's own elements collection then lists its controls by name:

Forms and their elements in JavaScript

document.forms.booking finds the form, booking.elements.guests finds the control named guests, and valueAsNumber reads its value as a number instead of a string. The console line below the preview shows that the button counts as one of the form's elements too. For pages with several forms, giving each a name (or an id) makes scripts much easier to read.

accept-charset

accept-charset once let a form choose the character encoding used for submitted text. Today the only valid value is UTF-8, which is also what browsers use when the page itself is UTF-8, as every modern page should be. You can safely leave it out. Character Encoding and UTF-8 explains why UTF-8 won.

A complete example

Here is a realistic upload form that uses several attributes together:

HTML
<form name="support" action="/support/tickets" method="post"
      enctype="multipart/form-data" autocomplete="on">
  <label for="t-email">Email</label>
  <input type="email" id="t-email" name="email" autocomplete="email" required>

  <label for="t-issue">Describe the problem</label>
  <textarea id="t-issue" name="issue" rows="6" required></textarea>

  <label for="t-shot">Screenshot (optional)</label>
  <input type="file" id="t-shot" name="screenshot" accept="image/*">

  <button>Open ticket</button>
</form>

It uses POST because it creates something, multipart because it carries a file, and a name so scripts can find it.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. A form has a file input but no enctype. What does the server receive for the file?

    The default URL-encoded format can only carry text, so only the file name is sent. File uploads need method=post and enctype=multipart/form-data.
  2. 2. What does novalidate on a <form> do?

    Controls keep their rules and validity state; the browser simply stops enforcing them on submit, usually so a script can show custom messages.
  3. 3. What happens when action is missing?

    An absent or empty action means the current document's URL.
  4. 4. Which method value closes a surrounding <dialog> instead of contacting a server?

    method=dialog is made for forms inside a <dialog> element. There is no close method.
  5. 5. Which value of accept-charset should modern pages use?

    UTF-8 is the only valid value today, and it is what browsers use anyway on UTF-8 pages.

Summary

  • action is the receiving URL (the current page if missing); method is get, post or dialog.
  • enctype="multipart/form-data" is required for file uploads; the default URL encoding carries text only.
  • target chooses where the response opens, just like on links.
  • autocomplete on the form sets a default for its controls; novalidate turns off the browser's validation UI.
  • name makes the form easy to reach through document.forms; its elements collection lists the controls.
  • accept-charset is effectively always UTF-8, so you can leave it out.

Next up: Form Validation, where the browser checks the data before it is sent.