What is Form Action in HTML: Simple Explanation and Example
form element's action attribute specifies the URL where the form data is sent when the user submits the form. It tells the browser what server or script will handle the information entered in the form.How It Works
Think of a form on a webpage like a letter you want to send. The action attribute is like the address on the envelope. It tells the browser exactly where to deliver the form's information once you click the submit button.
When you fill out a form and press submit, the browser collects all the data you entered and sends it to the URL specified in the action. This URL usually points to a server or script that processes the data, like saving it or sending an email.
If the action attribute is missing, the form data is sent to the same page that the form is on, which might reload the page or handle the data itself.
Example
This example shows a simple form that sends data to a server script located at /submit-form when submitted.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Form Action Example</title> </head> <body> <form action="/submit-form" method="post"> <label for="name">Name:</label> <input type="text" id="name" name="name" required> <br><br> <button type="submit">Send</button> </form> </body> </html>
When to Use
Use the action attribute whenever you want to send form data to a specific place for processing. This is common in contact forms, login pages, or any form where user input needs to be handled by a server.
For example, if you have a newsletter signup form, you set the action to the URL of the service that collects emails. If you build your own backend, the action points to your server script that saves or processes the data.
Key Points
- The
actionattribute defines where form data is sent on submit. - It usually contains a URL to a server or script that processes the data.
- If omitted, the form submits to the current page URL.
- Works together with the
methodattribute to control how data is sent.