HTML - action Attribute



HTML action attribute is used within a <form> element to specify where the form data should be sent when the form is submitted.

This attribute value can be overridden by the form tag's action attribute, which means if we specify this attribute with <input type = submit>, when the user submits the form, it will redirect according to the form tag's action 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

<form action = "URL"></form>

Applies On

Below listed element allow using of the HTML action attribute.

Element Description
<form> HTML <form> tag is used to specify the input field.

Examples of HTML action attribute

Here are some examples that shows different usage ways of action attribute

Action attribute with an existing URL

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. The below code will generate an output consisting of the input field along with a clickable button on the webpage. when the user clicks the button the form gets submitted successfully.

<!DOCTYPE html>
<html lang="en">

<head>
   <title>HTML 'action' attribute</title>
   <style>
      form {
         width: 250px;
         padding: 10px;
         background-color: skyblue;
         border-radius: 10px;
      }

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

      button {
         padding: 10px;
         width: 100px;
         cursor: pointer;
      }
   </style>
</head>

<body>
   <!--HTML 'action' attribute-->
   <h3>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>

Action attribute with wrong URL

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. The below code will generate an output consisting of the input field along with a clickable button displayed on the webpage. When the user tries to submit the form it will displays error because of the URL we have given.

<!DOCTYPE html>
<html lang="en">

<head>
   <title>HTML 'action' attribute</title>
   <style>
      form {
         width: 330px;
         padding: 10px;
         background-color: skyblue;
         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>

Supported Browsers

Attribute Chrome Edge Firefox Safari Opera
action Yes Yes Yes Yes Yes
html_attributes_reference.htm
Advertisements