HTML - formaction Attribute



The HTML formaction attribute is used to specify an URL of the file that will process the input controls and redirect to a different page when the form is submitted.

It overrides the action attribute of the form element. It is used with image and submit type of input elements and also used with <button> elements.

For example, if the image type input element has the formaction attribute present, when the user clicks on an image, it will redirect to a different page. And if the user submits the form, it will redirect to the URL specified in the action attribute of the <form> element.

Syntax

Following is the syntax for HTML formaction attribute −

<tag formaction = "URL"></tag>

Example

In the following example, we are going to use the HTML formaction attribute with input type=submit.

<!DOCTYPE html>
<html lang="en">
<head>
   <title>HTML 'formaction' attribute</title>
</head>
<body>
   <!--HTML 'formaction' attribute-->
   <p>Example of the HTML 'formaction' attribute</p>
   <form action="html logo.html" method="get">
      <label for="">Username</label>
      <input type="text">
      <br>
      <br>
      <label for="">Password</label>
      <input type="password">
      <br>
      <br>
      <input type="submit" value="Login" formaction="welcome.html">
   </form>
</body>
</html>

When we run the above code, it will generate an output consisting of the input field filled with data along with a click button displayed on the webpage.

Example

Considering the another scenario, where we are going to use the formaction attribute with the button element.

<!DOCTYPE html>
<html lang="en">
<head>
   <title>HTML 'formaction' attribute</title>
</head>
<body>
   <!--HTML 'formaction' attribute-->
   <p>Example of the HTML 'formaction' attribute</p>
   <form action="welcome1.html" method="get">
      <label for="">Name</label>
      <input type="text">
      <br>
      <br>
      <label for="">Email</label>
      <input type="email">
      <br>
      <br>
      <label for="">Mobile</label>
      <input type="number">
      <br>
      <br>
      <button>Submit</button>
      <button formaction="welcome.html">Submit to another page</button>
   </form>
</html>

On running the above code, the output window will pop up displaying the input field filled with data along with a click button on the webpage.

Example

Let's look at the following example, where we are going to use the formaction attribute with the input type=image.

<!DOCTYPE html>
<html lang="en">
<head>
   <title>HTML 'formaction' attribute</title>
</head>
<body>
   <!--HTML 'formaction' attribute-->
   <p>Example of the HTML 'formaction' attribute</p>
   <h3>Click on the below image to redirect different page</h3>
   <form action="https://www.tutorialspoint.com/static/images/logo.png?v3" method="get">
      <input type="image" formaction="download.png" src="https://www.tutorialspoint.com/static/images/logo.png?v3" width="200">
      <button>Submit</button>
      <p>Click on the above submit button to redirect different page.</p>
   </form>
</html>

When we run the above code, it will generate an output consisting of the text , image along with a click button on the webpage.

html_attributes_reference.htm
Advertisements