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
<form action="/send" method="post">
<input type="text" name="username" placeholder="Name">
<button type="submit">Submit</button>
</form>What happens:
- The user enters their name.
- When "Submit" is clicked, the browser builds a data set, for example:
username=Oleh- It sends them to the server at
/send(fromaction).
2. Possible values of action
| Value | What it does |
|---|---|
/send | Sends data to the specified path within the current site |
https://example.com/api | Sends data to an external server |
"" (empty) | Sends data to the current page |
| Attribute is missing | The same thing: the form submits to itself |
Example:
<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.
<form action="/send" method="post">4. If you specify a relative path
<form action="php/formHandler.php" method="post">→ the data will go to /current_folder/php/formHandler.php.
If an absolute address is needed:
<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:
<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:
| Attribute | Purpose |
|---|---|
action | Specifies the address (URL) the browser will send the form data to |
| Value | Relative or absolute URL |
| If not set | Submits to the page's current address |
| Works together with | method="get" or method="post" |
| Used for | Linking 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 readyA concise answer to help you respond confidently on this topic during an interview.