Form Action in HTML
Overview
In this article, you'll see how the action attribute is used with the form element and how to use form action in HTML. As you are familiar with the forms in HTML, we'll not cover forms in detail in this section. Refer here to learn more about forms in detail. In brief, HTML forms are used to collect different kinds of data, such as names, contact details, email addresses, etc., from the users.
Forms contain special elements called controls like submit buttons, checkboxes, etc. Similarly, an attribute named action on the form tag specifies where to send the form data. The form action in HTML is discussed along with the examples in this section.
HTML Form Action Attribute
Forms are used to accept the data from the user, and a form is useless unless some kind of processing takes place after submitting the form. To do this processing, there is an attribute called the action attribute, which specifies where to send the form data when submitted. In short, the action attribute or form action in HTML is used to inform the browser what page to call when the submit button is pressed.
The form data is mostly sent to a server-side handler, but it can also be sent to the JavaScript on the client. Let's see how you can use the form action in HTML while creating forms.
Syntax:
Value:
- URL: This is used to define the link or URL where we want to send the form data once the form is submitted. The URL can be anything like a URL to a document on the same server or an entirely different server, for example, a form-handling service.
Example
Consider an example below which explains how you can use the form action in HTML while developing web applications.
Output
Is The Action Attribute Required?
The action attribute was widely used in HTML4. Nowadays, in HTML5, you are not required to specify the action attribute. If you already have a form tag without an action attribute, then the data entered by the user will be sent to its own page i.e. in Single Page Application (SPA), you might want to process the form data using JavaScript, or you want another framework to process the data. Hence, in such cases, the action attribute is not required.
As Single Page Applications continue to grow in recent times, the data processing from the form is done using JavaScript. Or the data can be processed using other frameworks like React, Angular, etc.
Browser Support
-
Chrome 1.0 -- Sep 2008
-
Firefox 1.0 -- Sep 2002
-
IE/Edge 1.0 -- Aug 1995
-
Opera 1.0 -- Jan 2006
-
Safari 1.0 -- Jan 2003
Conclusion
- The HTML action attribute defines what happens when a form is submitted.
- The form action (attribute) in HTML accepts an argument.
- The value of the URL should be the URL.
- A detailed example is given to explain the form action in HTML.
- The action attribute was used in HTML4, but in HTML5, the action attribute is `no longer required.