HTML Input Form Attributes
The HTML <input> element can have attributes that override the behavior of its parent <form> element.
Overriding Form Behavior
These attributes are particularly useful for submit buttons, allowing a single form to have multiple submission behaviors based on which button the user clicks.
- formaction: Overrides the form's `action` attribute. Specifies the URL to send the form data to.
- formmethod: Overrides the form's `method` attribute. Can be GET or POST.
- formenctype: Overrides the form's `enctype` attribute (for file uploads).
- formnovalidate: Overrides the form's `novalidate` attribute.
- formtarget: Overrides the form's `target` attribute.
Example: The `formaction` Attribute
This example shows a form with two submit buttons. The second button overrides the form's default action and submits to a different page.
html
<form action="/action_page.php">
<label for="fname">First name:</label>
<input type="text" id="fname" name="fname"><br><br>
<input type="submit" value="Submit">
<input type="submit" formaction="/action_page2.php" value="Submit to another page">
</form>Example: The `formmethod` Attribute
This example shows a form with two submit buttons, each using a different HTTP method.
html
<form action="/action_page.php" method="get">
<label for="fname">First name:</label>
<input type="text" id="fname" name="fname"><br><br>
<input type="submit" value="Submit using GET">
<input type="submit" formmethod="post" value="Submit using POST">
</form>Test Yourself with an Exercise
Which attribute overrides the form's `action` attribute for a specific submit button?