References

Beginner-friendly references for web development, with live, editable examples.

The HTML action attribute

Attribute All modern browsers
In short

The action attribute of a <form> holds the URL that receives the form's data, such as action="/search". Leave it out and the form submits to its own page. A formaction on the button that was pressed takes priority.

Overview

The HTML action attribute names the URL that processes a form submission. You set it on the <form> element, either as an absolute URL, such as https://example.com/search, or as a relative one, such as /search.

When action is missing or empty, the form submits to the URL of the page it is on. When the attribute is present, it must hold a non-empty URL, so action="" is invalid markup, even though it submits the same way.

With method="get", the browser replaces the query string of an http or https action with the form's fields. An action of /search?lang=en with tea in the q field loads /search?q=tea, so lang=en is lost. A POST form keeps the query string and sends its fields in the request body.

A submit button can send the form somewhere else with its own formaction attribute, which overrides action for that button only. A submission that uses method="dialog" ignores action altogether, because the browser never reads it in that case.

Syntax

<form action="/search" method="get">
  <label for="q">Search recipes</label>
  <input type="search" id="q" name="q">
  <button>Search</button>
</form>

Values

Value
A URL.

Example

Live example
<form id="search" action="https://example.com/search?lang=en">
  <label for="q">Search recipes</label>
  <input type="search" id="q" name="q" value="lentil soup">
  <p>
    <button>Search</button>
    <button formaction="https://example.com/images">Images</button>
  </p>
</form>
<p id="out" role="status" style="overflow-wrap:anywhere"></p>
<p>Press each button to see the address the browser would load. The lang=en part of the action never reaches it.</p>
<script>
const form = document.getElementById('search');
form.addEventListener('submit', function (e) {
  e.preventDefault();
  const button = e.submitter;
  const target = button && button.hasAttribute('formaction') ? button.formAction : form.action;
  const url = new URL(target);
  url.search = new URLSearchParams(new FormData(form)).toString();
  document.getElementById('out').textContent = 'GET ' + url.href;
});
</script>

Best practices

  • Point action at an https URL. Browsers display a security warning each time a form on a secure page would send its data to an http address, because that data isn't encrypted.
  • Keep fixed values for a GET form, such as a language, in hidden inputs like <input type="hidden" name="lang" value="en">. Hidden fields are sent, while a query string written in action is not.
  • Read form.action when a script needs the form's destination, since it returns the full URL with a relative value already resolved. An input named action replaces that property, so use getAttribute('action') in such a form.
  • Check button.hasAttribute('formaction') before you rely on button.formAction. When the button has no formaction, that property returns the page's own URL rather than the form's action.
  • Restrict form targets with a Content-Security-Policy form-action directive when forms should only reach your own site. With form-action 'self', a submission to another origin is blocked.

Frequently asked questions

What is a form's action attribute?
The action attribute holds the address that receives a form's data when someone submits it. The browser requests that URL with GET or POST, depending on the form's method.
Where does a form submit without an action attribute?
A form without an action attribute submits to the URL of its own page. In a script, form.action returns that page's URL in this case.
Is a relative form action resolved against the page or the site root?
A relative action resolves against the page's base URL, as a link's href does. On /shop/list, action=results goes to /shop/results, while action=/results goes to /results at the site root.
Does a GET form keep the #fragment of its action URL?
Yes, a GET submission replaces only the query of the action URL, so a fragment stays. With tea in the q field, /search#results loads /search?q=tea#results.
Does assigning form.action in JavaScript rewrite the attribute?
Yes, setting the form's action property writes the new URL into the action attribute. Later submissions then go to that URL.