HTML Submit formAction Property: Submit Button Form Action
The HTML formAction property for submit buttons allows you to override the form’s action attribute. This is particularly useful when you need a single form to submit data to different endpoints based on which submit button is clicked. It provides a flexible way to handle various form submission scenarios without creating multiple forms.
Definition and Purpose
The formAction attribute specifies the URL to which the form data will be sent when the submit button is clicked. It overrides the form’s action attribute, allowing different submit buttons within the same form to submit data to different URLs. This is especially helpful in scenarios where you need different actions, such as saving, submitting, or previewing data, all within the same form context.
Syntax
The syntax for the formAction attribute is straightforward:
<button type="submit" formAction="URL">Submit Button</button>
Here, "URL" is the address to which the form data will be submitted when this specific button is pressed.
Attributes
The formAction attribute accepts a URL as its value, which specifies the endpoint for form submission.
| Attribute | Value | Description |
|---|---|---|
| `formAction` | URL | Specifies the URL to which the form data is sent when the button is clicked. Overrides the form’s `action` attribute. |
Examples
Let’s explore some practical examples to understand how the formAction attribute works.
Basic Usage: Overriding Form Action
In this example, we have a form with a default action, but one of the submit buttons overrides this to submit to a different URL.
<form action="/default-submit" method="post" id="myForm1">
<label for="name">Name:</label><br />
<input type="text" id="name" name="name" /><br /><br />
<button type="submit">Submit</button>
<button type="submit" formAction="/alternative-submit">
Alternative Submit
</button>
</form>
In this case, clicking the “Submit” button will send the form data to /default-submit, while clicking “Alternative Submit” will send it to /alternative-submit.
Multiple Actions in a Single Form
This example demonstrates how to use formAction to handle different actions within the same form.
<form action="/default-submit" method="post" id="myForm2">
<label for="email">Email:</label><br />
<input type="email" id="email" name="email" /><br /><br />
<button type="submit" formAction="/save-draft">Save Draft</button>
<button type="submit" formAction="/submit-form">Submit Form</button>
<button type="submit" formAction="/preview">Preview</button>
</form>
Here, each button submits the form to a different endpoint: /save-draft, /submit-form, and /preview, allowing for distinct actions based on user choice.
Using Absolute URLs
You can also use absolute URLs with the formAction attribute to submit data to external websites or APIs.
<form id="myForm3">
<label for="comment">Comment:</label><br />
<textarea id="comment" name="comment"></textarea><br /><br />
<button type="submit" formAction="https://api.example.com/submit-comment">
Submit Comment
</button>
</form>
In this example, the form data is submitted to an external API endpoint at https://api.example.com/submit-comment.
Combining with Other Form Attributes
The formAction attribute can be combined with other form-related attributes like formMethod, formTarget, and formEnctype for even more control over the submission process.
<form action="/default-submit" method="post" id="myForm4">
<label for="message">Message:</label><br />
<textarea id="message" name="message"></textarea><br /><br />
<button
type="submit"
formAction="/submit-ajax"
formMethod="get"
formTarget="_blank"
>
Submit via Ajax
</button>
</form>
Here, the button overrides the form’s default action and method, submitting the data to /submit-ajax using the GET method and opening the response in a new tab (_blank).
Tips and Notes
- Using
formActionprovides a clean and organized way to handle multiple submission scenarios within a single form. - Ensure the URLs specified in
formActionare correctly configured to handle the incoming form data. - Consider using descriptive button labels to clearly indicate the action that will be performed when the button is clicked.
- When using absolute URLs, be mindful of CORS (Cross-Origin Resource Sharing) policies if submitting to external domains.
Browser Support
The formAction attribute is supported by all modern browsers, including:
- Chrome
- Edge
- Firefox
- Safari
- Opera
Conclusion
The formAction attribute for submit buttons is a powerful tool for creating flexible and dynamic forms. By allowing different submit buttons to target different URLs, you can handle a variety of submission scenarios within a single form, improving both user experience and code maintainability.








