formaction

The formaction attribute defines the URL where the form data should be submitted when using <button> or <input type="submit">, overriding the form's default submission action.

formaction attribute

The formaction attribute in HTML can be applied to <button> or <input type="submit"> elements to specify the URL where the form data should be sent upon submission. This attribute overrides the action attribute of the parent form, allowing different submit buttons within the same form to direct data to different URLs.

Syntax

index.html
<tagname type="submit | image" formaction="URL" >

Example

Here’s an example of using the formaction attribute:

index.html
<form>
  <input type="text" name="username" placeholder="Username">
  <input type="text" name="password" placeholder="Password">
  <button type="submit" formaction="/login">Login</button>
  <button type="submit" formaction="/register">Register</button>
</form>

In this example, clicking "Login" submits the form data to /login, while clicking "Register" sends it to /register.

Values

  • URL
    • A target URL where the form data is sent for processing.

Applies To

The formaction attribute is applicable to the following HTML elements.

Example

index.html
<form>
  <input type="text" name="name" placeholder="Enter your name">
  <input type="submit" value="Submit to Products">
  <input type="submit" value="Submit to Contact Us">
</form>

Conclusion

The formaction attribute enables you to define a specific URL for submitting form data, bypassing the form's default action. It works with <button> and <input type="submit"> elements, allowing multiple submit buttons within the same form to send data to different destinations. This ensures that the data is directed to the appropriate endpoint based on the button pressed.