On this page12 sections
Lecture 034: Form Attributes
| Category | 6: Forms |
| Lecture | 034 of 048 |
| Estimated video length | 10 min |
| Target | HTML5 · any modern browser |
| Level | Beginner |
The
<form>element itself has attributes that decide what happens when the form is sent: where the data goes, how it travels, whether it opens a new tab, and whether the browser offers to auto-fill or checks the answers.
Learning Objectives
- Set where data goes with
action, and how withmethod - Choose between GET and POST
- Use
target,autocomplete,novalidate,enctypeandname
1. action: Where the Data Goes
<form action="/subscribe">
…
</form>action is the URL of the page (usually a server script) that receives the data. Without
it, the form sends the data to the page it's on.
2. method: GET or POST
method="get" (default) | method="post" | |
|---|---|---|
| Where the data goes | In the URL: /search?q=tea | Hidden in the request body |
| Can be bookmarked or shared | Yes | No |
| Length limit | Yes (about 2,000 characters) | No practical limit |
| Use for | Searches and filters | Logins, sign-ups, payments, anything private or that changes data |
Example: a GET search form. Submit it to see the data:
<form action="/search" method="get">
<input name="q" placeholder="Search recipes">
<button type="submit">Search</button>
</form>3. target: Where the Response Opens
<form action="/report" target="_blank">| Value | Opens in… |
|---|---|
_self (default) | The same tab |
_blank | A new tab |
| A frame name | That <iframe> |
4. autocomplete
Lets the browser suggest values the visitor typed before. Can be set on the form or on each field.
Example
<form autocomplete="on">
<p><label>Name <input name="name" autocomplete="name"></label></p>
<p><label>Email <input name="email" type="email" autocomplete="email"></label></p>
<p><label>One-time code <input name="otp" autocomplete="off"></label></p>
</form>Specific values such as name, email, tel, street-address and current-password help
browsers and password managers fill in the right thing.
5. novalidate
Turns off the browser's built-in checks, for example when you validate with your own JavaScript instead.
Example: this submits even when empty:
<form novalidate>
<input type="email" name="email" required placeholder="Required, but not checked">
<button type="submit">Send</button>
</form>6. enctype: Sending Files
To upload files, use POST and multipart/form-data:
<form action="/upload" method="post" enctype="multipart/form-data">
<input type="file" name="photo">
<button type="submit">Upload</button>
</form>Quick Reference
| Attribute | Purpose |
|---|---|
action | URL that receives the data |
method | get or post |
target | Where to show the response |
autocomplete | on / off: browser suggestions |
novalidate | Skip built-in validation |
enctype | How data is encoded (multipart/form-data for files) |
name | A name for the form, for scripts |
accept-charset | Character set for the data (UTF-8 by default) |
Common Beginner Mistakes
1. Passwords with GET. They end up in the URL, the browser history and server logs. Use POST.
2. File uploads without enctype. The server receives only the file name, not the file.
3. autocomplete="off" everywhere. It annoys visitors and confuses password managers.
Practice Questions
- Which attribute sets where form data is sent?
- What is the default
method? - Which method should a login form use, and why?
- How do you make the response open in a new tab?
- Which two attributes are needed for a file upload form?
- What does
novalidatedo?
Practice Answers
actiongetpost, so the password isn't shown in the URL or saved in history.target="_blank"method="post"andenctype="multipart/form-data".- It turns off the browser's built-in validation.
Key Takeaways
action= where;method= how (getfor searches,postfor private data).targetchooses where the response opens.autocompletehelps visitors;novalidateskips browser checks.- File uploads need
post+enctype="multipart/form-data".