<form>
The <form> element is a container for user input that submits data to a server. You specify the destination URL and submission method using attributes.
Syntax
<form action="destination-url" method="post"> <!-- Place form controls here --> <input type="text" name="username"> <button type="submit">Submit</button> </form>
Attributes
| Attribute | Description |
|---|---|
| action | Specifies the URL to which the form data is submitted. If omitted, the data is sent to the current page's URL. |
| method | Specifies the submission method. Use get (appends data to the URL) or post (includes data in the request body). |
| enctype | Specifies the encoding type for the submitted data. Use multipart/form-data when the form includes file uploads. |
| novalidate | When present, disables the browser's built-in form validation. |
| autocomplete | Enables (on) or disables (off) autocomplete for the entire form. |
| target | Specifies the browsing context in which to display the response. Use _blank to open it in a new tab. |
Sample Code
<!-- Basic contact form -->
<form action="/contact.php" method="post" autocomplete="on">
<div>
<label for="name">Name (required)</label>
<input type="text" id="name" name="name" required>
</div>
<div>
<label for="email">Email address (required)</label>
<input type="email" id="email" name="email" required>
</div>
<div>
<label for="message">Message</label>
<textarea id="message" name="message" rows="5"></textarea>
</div>
<button type="submit">Send</button>
</form>
<!-- File upload form -->
<form action="/upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<button type="submit">Upload</button>
</form>
Output
A text field for name, an email field, a textarea, and a submit button are displayed. Clicking "Send" validates the input, and if there are no errors, the data is submitted via POST to /contact.php.
Notes
Use method="get" when you want the data to appear in the URL, such as for search forms. Use method="post" when submitting sensitive data such as passwords or personal information that should not be visible in the URL. Always use method="post" for forms that handle passwords or personal information, and make sure the page is served over HTTPS.
The default value of the enctype attribute is application/x-www-form-urlencoded, which works fine for ordinary text data. If the form includes a file upload, you must specify multipart/form-data. Without it, the file will not be submitted correctly.
For input controls you can place inside a form (such as text fields), see input (text-based).
Browser Compatibility
2 or earlier ×
14 or earlier ×
Android Browser
37+ ○
4 or earlier ×
Chrome Android
36+ ○
17 or earlier ×
Firefox Android
79+ ○
3 or earlier ×If you find any errors or copyright issues, please contact us.