HTML: the action attribute
The action attribute tells a form which URL should receive its submitted data.
What you will learn
- How to set a form destination with
action - Why
methodand controlnamevalues affect the request - How
formactioncan choose a different destination for one submit button
Basic example
<form action="/search" method="get">
<label>Keyword <input name="q"></label>
<button type="submit">Search</button>
</form>
When the form is submitted, the browser sends the successful controls to /search. The name value becomes the parameter name, so a keyword might produce /search?q=html.
action and method
actionselects the destination URL.method="get"places form data in the URL, which is useful for searches and filters.method="post"sends data in the request body and is commonly used when creating or changing server-side data.- If
actionis omitted, the form normally submits to the current document URL.
Different destinations for buttons
<form action="/orders/submit" method="post">
<button type="submit">Place order</button>
<button type="submit" formaction="/orders/draft">Save draft</button>
</form>
The second button overrides the form's action for that submission. This is useful when related actions share the same fields but should use different endpoints.
Common mistakes
- Expecting an input without a
nameto be included in submitted data. - Using
getfor secrets or other data that should not appear in a URL. - Changing the destination only with JavaScript and leaving the no-script form unclear.