The HTML action attribute
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
<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.actionwhen 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 usegetAttribute('action')in such a form. - Check
button.hasAttribute('formaction')before you rely onbutton.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-actiondirective when forms should only reach your own site. Withform-action 'self', a submission to another origin is blocked.























