HTML Form Attributes
The <form> element has several attributes that control where the data goes, how it is sent and how the browser should behave while the user fills it in. This lesson goes through each of them.
Overview
| Attribute | Example | Purpose |
|---|---|---|
action |
action="/subscribe" |
URL that receives the submitted data |
method |
method="post" |
HTTP method: get (default) or post |
target |
target="_blank" |
Where to show the response |
enctype |
enctype="multipart/form-data" |
How the data is encoded (POST only) |
autocomplete |
autocomplete="off" |
Let the browser suggest saved values or not |
novalidate |
novalidate |
Skip built-in validation on submit |
name |
name="login" |
Name for the form, usable from scripts |
accept-charset |
accept-charset="UTF-8" |
Character encoding for the data |
action
action is the address of the server program that will process the data. If you leave it out, the form submits to the current page's URL. In these playgrounds there is no server, so examples use action="#" or stop the submission with JavaScript.
method: GET vs POST
The method attribute decides how the data travels.
| GET | POST | |
|---|---|---|
| Where data goes | Appended to the URL: ?q=html&page=2 |
Inside the request body |
| Visible in address bar | Yes | No |
| Can be bookmarked | Yes | No |
| Size limit | Around a few thousand characters | Very large (files are fine) |
| Good for | Searches, filters | Logins, payments, anything that changes data |
<style>
code { background: #eef; padding: 2px 4px; }
</style>
<form id="search" action="https://example.com/search" method="get">
<input name="q" value="html forms">
<select name="lang">
<option>en</option><option selected>vi</option>
</select>
<button>Search</button>
</form>
<p>A GET request would go to:<br><code id="url"></code></p>
<script>
// The sandbox can't navigate to a real server, so we just show the URL.
const f = document.getElementById('search');
f.addEventListener('submit', (e) => {
e.preventDefault();
const query = new URLSearchParams(new FormData(f)).toString();
document.getElementById('url').textContent = f.action + '?' + query;
});
</script>Tip: Never send passwords with GET. They would appear in the URL, the browser history and server logs.
target
target works just like it does on links: _self (default) shows the response in the same frame, _blank opens a new tab, and you can also give the name of an <iframe> to load the response inside it.
enctype
With method="post" you can choose how the body is encoded:
| Value | Use |
|---|---|
application/x-www-form-urlencoded |
Default; text is percent-encoded |
multipart/form-data |
Required when the form contains <input type="file"> |
text/plain |
Plain text; only useful for debugging |
autocomplete
Browsers remember values you typed before and offer to fill them in again. autocomplete="off" on the form (or an individual input) asks the browser not to. You can also be more helpful by using specific tokens on inputs, like autocomplete="email" or autocomplete="postal-code", so the browser fills the right data in the right field.
<form action="#" autocomplete="on">
<p><label>Email <input type="email" name="email" autocomplete="email"></label></p>
<p><label>One-time code <input name="otp" autocomplete="off"></label></p>
<p><label>Street <input name="street" autocomplete="street-address"></label></p>
<button>Save</button>
</form>novalidate
Normally the browser blocks submission if a field breaks a rule such as required. Adding novalidate to the form turns that check off — handy when you want to show your own error messages with JavaScript. Compare the two forms below: leave the field empty and press each button.
<style>
form { border: 1px solid #ccc; padding: 8px; margin-bottom: 8px; }
</style>
<form id="a">
<strong>With validation</strong><br>
<input name="nick" required placeholder="required">
<button>Send</button>
</form>
<form id="b" novalidate>
<strong>With novalidate</strong><br>
<input name="nick" required placeholder="required">
<button>Send</button>
</form>
<p id="log"></p>
<script>
for (const id of ['a', 'b']) {
document.getElementById(id).addEventListener('submit', (e) => {
e.preventDefault();
document.getElementById('log').textContent = 'Form "' + id + '" was submitted!';
});
}
</script>Form "a" refuses to submit when empty; form "b" submits anyway.
Putting it together
<form action="/upload" method="post" enctype="multipart/form-data" target="_blank">
<input type="file" name="avatar">
<button>Upload</button>
</form>This form posts a file to /upload using multipart encoding and shows the server's answer in a new tab.
Note: Individual buttons can override these settings with
formaction,formmethod,formtarget,formenctypeandformnovalidate. You will meet them in the Input Attributes lesson.
Test yourself
Which method should a login form use?
getpostputsend
Which enctype is required for a form that uploads files?
text/plainapplication/jsonmultipart/form-dataapplication/x-www-form-urlencoded
What does the novalidate attribute on <form> do?
- Removes all inputs
- Disables the submit button
- Skips the browser's built-in validation on submit
- Validates on the server instead