HTML action Attribute
The HTML action attribute specifies where the form-data should be sent when the form is submitted. Read and find out on what element you can use this attribute.
The HTML action attribute specifies the URL where the form data should be sent when the form is submitted. Its value is the destination that processes the submitted data — usually a server-side script or endpoint.
You can use this attribute only on the <form> element. When a user activates a submit control, the browser gathers the form's named fields, encodes them, and sends a request to the address in action. This page explains how action behaves, what happens when you leave it out, how it interacts with other form attributes, and how a single button can override it with formaction.
Syntax
<form action="URL"></form>The URL can be one of two kinds:
- Absolute — a full URL including the scheme and host, for example
https://api.example.com/submit. Use this when the form must post to a different origin (a different domain, subdomain, or scheme). - Relative — a path resolved against the current document's URL, for example
/form/submitorsave.php. This is the common choice when the form posts back to your own site.
Example
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
</head>
<body>
<form action="/form/submit">
<label for="fname">Name</label>
<input type="text" name="FirstName" id="fname" value="Mary"/><br /><br />
<label for="lname">Surname</label>
<input type="text" name="LastName" id="lname" value="Thomson"/><br /><br />
<input type="submit" value="Submit"/>
</form>
</body>
</html>Here we use a relative path, so the data is sent to /form/submit on the same domain as the current page. To post to another domain, use an absolute URL such as https://example.com/some-page.
What happens when action is omitted
If you leave out action (or set it to an empty string), the form submits to the current page's URL. This is a valid and common pattern: a page can serve the form and also handle its own submission.
<!-- Both of these submit back to the URL the form was loaded from -->
<form method="post">...</form>
<form action="" method="post">...</form>Keep in mind that submitting to the current URL with the default GET method appends the form fields to the URL as a query string, which reloads the page with those values visible in the address bar.
How action interacts with other form attributes
The action attribute never works alone — it defines where data goes, while other attributes on <form> define how it gets there:
method— chooses the HTTP method. WithGET(the default), the encoded fields are appended to theactionURL as a query string, so the URL becomesaction?name=value&.... WithPOST, the fields are sent in the request body, keeping the URL clean.enctype— sets how the body is encoded, and only matters withPOST. The defaultapplication/x-www-form-urlencodedis fine for text; you needmultipart/form-datawhen the form includes a file upload (<input type="file">).target— decides where the response is shown, for example_self(same tab, default),_blank(new tab), or the name of an<iframe>.
You can learn more about these on the <form> tag reference and the broader HTML forms guide.
Absolute vs. relative URLs: trade-offs
Choosing between an absolute and a relative action is more than a style preference:
- Relative URLs keep the form tied to the site that served it. They survive moving the site to a new domain or switching between
httpandhttps, and they keep the submission same-origin, which avoids cross-origin complications. - Absolute URLs are required when you must post to a different origin (for example a third-party form handler or a separate API host). Be deliberate here: a
POSTto another origin is a cross-origin request. The receiving server must accept it, and the request may be subject to CORS rules for the scripted parts of your page.
A security note: forms can submit data to any origin you put in action, and browsers will send that origin's cookies along with the request. This is exactly the mechanism behind cross-site request forgery (CSRF) — a malicious page can host a form that posts to your site. Because of this, only set action to origins you trust, and protect any state-changing endpoint with an anti-CSRF token validated on the server. Prefer relative (same-origin) actions unless you have a concrete reason to post elsewhere.
Overriding action with formaction
A single form can have more than one submit button, and each can send the data to a different place using the formaction attribute. When present on the button or input that triggered submission, formaction overrides the form's own action. (The related formmethod, formenctype, and formtarget attributes override method, enctype, and target the same way.)
<!DOCTYPE html>
<html>
<head>
<title>formaction example</title>
</head>
<body>
<form action="/articles/publish" method="post">
<label for="title">Title</label>
<input type="text" name="title" id="title" value="My draft"/><br /><br />
<!-- Uses the form's action: /articles/publish -->
<button type="submit">Publish</button>
<!-- Overrides the action just for this button -->
<button type="submit" formaction="/articles/save-draft">Save draft</button>
</form>
</body>
</html>Both buttons submit the same fields, but "Publish" posts to /articles/publish while "Save draft" posts to /articles/save-draft. formaction is supported on <button type="submit"> and <input type="submit"> (and <input type="image">), and it has no effect on non-submit controls.