HTML - action Attribute



The HTML action attribute is used to specify a URL that processes the form submission. It defines an action to be performed when the form is submitted.

This attribute value can be overridden by the formaction attribute, which means if we specify this attribute with <input type = submit>, when the user submits the form, it will redirect according to the formaction attribute source URL, not according to the action attribute.

The ‘action’ attribute works with the <form> element. This attribute is ignored when the method =dialog attribute is set within the <form> element.

Syntax

Following is the syntax for HTML 'action’ attribute −

<form action = "value"></form>

Example

In the following example, we are using the HTML ‘action’ attribute within the <form> element to define the action to be performed when the form is submitted.

<!DOCTYPE html>
<html lang="en">
<head>
   <title>HTML 'action' attribute</title>
   <style>
      form {
         width: 250px;
         padding: 10px;
         background-color: aquamarine;
         border-radius: 10px;
      }

      input {
         padding: 10px;
         width: 200px;
      }

      button {
         padding: 10px;
         width: 100px;
         cursor: pointer;
      }
   </style>
</head>
<body>
   <!--HTML 'action' attribute-->
   <h3>Example of the HTML 'action' attribute</h3>
   <form action="welcome.html" method="GET">
      <h1>Login Page</h1>
      <input type="text" placeholder="Username" name="uname" required>
      <br>
      <br>
      <input type="password" placeholder="Password" name="password" required>
      <br>
      <br>
      <button>Login</button>
   </form>
</body>
</html>

On running the above code, it will generate an output consisting of the input field along with a click button on the webpage. when the user click the button the form gets submitted successfully.

Example

Considering the another scenario, where we are going to use the action attribute with the form tag and assign the wrong URL which displays error on submission of the form.

<!DOCTYPE html>
<html lang="en">
<head>
   <title>HTML 'action' attribute</title>
   <style>
      form {
         width: 330px;
         padding: 10px;
         background-color: rgb(7, 32, 158);
         border-radius: 10px;
         color: white;
      }

      input {
         padding: 12px;
         width: 300px;
      }

      button {
         padding: 12px;
         width: 100px;
         cursor: pointer;
      }

      form h1 {
         text-align: center;
         font-family: sans-serif;
         letter-spacing: 2px;
      }
   </style>
</head>
<body>
   <!--HTML 'action' attribute-->
   <h3>Example of the HTML 'action' attribute</h3>
   <form action="/action_page.php" method="GET">
      <h1>Login</h1>
      <input type="text" placeholder="Username" name="uname" required>
      <br>
      <br>
      <input type="password" placeholder="Password" name="password" required>
      <br>
      <br>
      <button>Login</button>
   </form>
</body>
</html>

When we run the above code, it will generate an output consisting of the input field along with a click button displayed on the webpage. when the user tries to submit the form it will displays error because of the URL we have given.

html_attributes_reference.htm
Advertisements