Skip to main content

HTML Forms

HTML Form Attributes

Written by Updated HTML Form Attributes exercises

The attributes on a form tag decide what happens when someone presses Submit: where the data goes, how it travels, and whether files can come along. Get enctype wrong and an upload arrives empty; leave out method and a password ends up in the address bar.

Each attribute below has its own example. Attributes for the fields themselves, like required and placeholder, are in HTML Input Attributes. The basics of how forms work are in the HTML form overview.

The action Attribute

The address that receives the data, usually a script on the server. If you leave it out, the form sends the data back to the same page.

Form Action Attribute

html

<form action="/samples/form_processor.php">
  <input type="text" name="username">
  <input type="submit">
</form>

The method Attribute

How the data is sent. There are two choices:

  • GET adds the data to the address, like /search?q=html. Good for searches, because the result can be bookmarked and shared. Never for passwords or personal data.
  • POST sends the data hidden in the request. Use it for logins, sign-ups, payments and anything that changes something on the server.

The default is GET, which is why forgetting method="post" is a real security mistake.

Form Method Attribute

html

<form action="/samples/form_processor.php" method="post">
  <input type="text" name="email">
  <input type="submit" value="Submit">
</form>

The target Attribute

Where the server's answer is shown.

ValueOpens the result in
_selfThe same tab. The default.
_blankA new tab.
_parentThe parent frame, if the form is inside an iframe.
_topThe full window, breaking out of any frames.

Form Target Attribute

html

<form action="/samples/form_processor.php" target="_blank">
  <input type="text" name="query">
  <input type="submit" value="Search">
</form>

The enctype Attribute

How the data is packaged. It only matters with POST.

ValueWhen to use it
application/x-www-form-urlencodedThe default. Fine for normal text fields.
multipart/form-dataRequired for file uploads. Without it, the server receives only the file name.
text/plainAlmost never; mainly for debugging.

Form Enctype Attribute

html

<form action="/samples/form_processor.php" method="post" enctype="multipart/form-data">
  <input type="file" name="fileUpload">
  <input type="submit" value="Upload">
</form>

The autocomplete Attribute

Turns the browser's saved suggestions on or off for every field in the form. You can override it on individual inputs. Note that browsers often ignore off for login fields, because password managers depend on them.

Form Autocomplete Attribute

html

<form autocomplete="off">
  <input type="text" name="name" placeholder="Full name">
  <input type="email" name="email" placeholder="Email address">
  <input type="submit">
</form>

The novalidate Attribute

Switches off the browser's built-in form validation, so required and type="email" no longer block submission. Useful while testing, or when your own JavaScript shows the error messages.

Form Novalidate Attribute

html

<form action="/samples/form_processor.php" novalidate>
  <input type="email" name="email" placeholder="Enter your email">
  <input type="submit">
</form>

The name Attribute

A name for the form, which lets JavaScript find it with document.forms.registrationForm. It is not sent to the server.

Form Name Attribute

html

<form name="registrationForm">
  <input type="text" name="username">
  <input type="submit">
</form>

The rel Attribute

Describes the link between your page and the page the form goes to, the same way rel works on links. You will rarely need it; rel="noopener" is the most common use, for forms that open in a new tab.

Form Rel Attribute

html

<form action="/samples/form_processor.php" rel="external">
  <input type="text" name="data">
  <input type="submit">
</form>

The accept-charset Attribute

The character encoding the server expects. Today that is always UTF-8, which is already the default for UTF-8 pages, so this attribute is mostly found in old code.

Form accept-charset Attribute

html

<form action="/samples/form_processor.php" accept-charset="UTF-8">
  <input type="text" name="comment">
  <input type="submit">
</form>

All together

Different Form Attributes Example

html

<form action="/samples/form_processor.php" method="post" target="_self"
      enctype="multipart/form-data" autocomplete="on" name="userForm">
  
  <label>Full Name:</label>
  <input type="text" name="fullname" required><br><br>

  <label>Email:</label>
  <input type="email" name="email" required><br><br>

  <label>Profile Picture:</label>
  <input type="file" name="profile_pic"><br><br>

  <label>Message:</label><br>
  <textarea name="message" rows="4" cols="30"></textarea><br><br>

  <input type="submit" value="Submit Form">
</form>

That is a typical sign-up form: posted to a script, kept in the same tab, able to accept a file, with suggestions on.

Browser Support

Chrome
Edge
Firefox
Safari
Opera
IE9+
YesYesYesYesYesYes

Good to know

  • A form with a file input and no enctype="multipart/form-data" is the classic upload bug.
  • method="get" is right for search forms. Everything else that sends personal data should use POST.
  • Buttons and inputs can override some of these per button, with formaction, formmethod and formnovalidate. That lets one form have a "Save draft" button that skips validation.

HTML Form Attributes exercises

Answer in a new tab

Make a form that sends to /report, opens the answer in a new tab, and has a submit button that says Show report.

Result

Check your answer to see it here.