Skip to main content

What does the action attribute do on <form>?

The action attribute on the <form> tag specifies the address (URL) to which the form data will be sent after the Submit button is clicked (<button type="submit">).

This is the address of a handler (usually a server-side script, PHP, Python, Node.js, and so on) that accepts and processes the data entered by the user.


1. Example

html
<form action="/send" method="post"> <input type="text" name="username" placeholder="Name"> <button type="submit">Submit</button> </form>

What happens:

  1. The user enters their name.
  2. When "Submit" is clicked, the browser builds a data set, for example:
javascript
username=Oleh
  1. It sends them to the server at /send (from action).

2. Possible values of action

ValueWhat it does
/sendSends data to the specified path within the current site
https://example.com/apiSends data to an external server
"" (empty)Sends data to the current page
Attribute is missingThe same thing: the form submits to itself

Example:

html
<form action=""> <!-- the form will submit data to the current URL --> </form>

3. How it works together with method

  • method="get" → the data is appended to the URL (in the browser's address bar):

    javascript
    /send?username=Oleh
  • method="post" → the data is sent "in the body" of the HTTP request, not visible in the address bar.

html
<form action="/send" method="post">

4. If you specify a relative path

html
<form action="php/formHandler.php" method="post">

→ the data will go to /current_folder/php/formHandler.php.

If an absolute address is needed:

html
<form action="https://mysite.com/api/contact" method="post">

5. Usage with JavaScript

If processing happens not on the server but through JavaScript (for example, AJAX), action can be ignored: the script decides on its own where to send the data.

Example:

html
<form id="form"> <input name="email"> <button>OK</button> </form> <script> document.getElementById('form').addEventListener('submit', e => { e.preventDefault(); // cancel the default submission console.log('Form submitted manually via JS'); }); </script>

Summary:

AttributePurpose
actionSpecifies the address (URL) the browser will send the form data to
ValueRelative or absolute URL
If not setSubmits to the page's current address
Works together withmethod="get" or method="post"
Used forLinking the form to a server-side data handler

In simple terms: action is "where to send the letter." The form gathers data, and action tells the browser:

"Here is the recipient's address, take everything the user entered there."

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.