On this page12 sections
  1. Learning Objectives
  2. 1. action: Where the Data Goes
  3. 2. method: GET or POST
  4. 3. target: Where the Response Opens
  5. 4. autocomplete
  6. 5. novalidate
  7. 6. enctype: Sending Files
  8. Quick Reference
  9. Common Beginner Mistakes
  10. Practice Questions
  11. Practice Answers
  12. Key Takeaways

Lecture 034: Form Attributes

Category6: Forms
Lecture034 of 048
Estimated video length10 min
TargetHTML5 · any modern browser
LevelBeginner

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 with method
  • Choose between GET and POST
  • Use target, autocomplete, novalidate, enctype and name

1. action: Where the Data Goes

html
<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 goesIn the URL: /search?q=teaHidden in the request body
Can be bookmarked or sharedYesNo
Length limitYes (about 2,000 characters)No practical limit
Use forSearches and filtersLogins, sign-ups, payments, anything private or that changes data

Example: a GET search form. Submit it to see the data:

html
<form action="/search" method="get">
  <input name="q" placeholder="Search recipes">
  <button type="submit">Search</button>
</form>

3. target: Where the Response Opens

html
<form action="/report" target="_blank">
ValueOpens in…
_self (default)The same tab
_blankA new tab
A frame nameThat <iframe>

4. autocomplete

Lets the browser suggest values the visitor typed before. Can be set on the form or on each field.

Example

html
<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:

html
<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:

html
<form action="/upload" method="post" enctype="multipart/form-data">
  <input type="file" name="photo">
  <button type="submit">Upload</button>
</form>

Quick Reference

AttributePurpose
actionURL that receives the data
methodget or post
targetWhere to show the response
autocompleteon / off: browser suggestions
novalidateSkip built-in validation
enctypeHow data is encoded (multipart/form-data for files)
nameA name for the form, for scripts
accept-charsetCharacter 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

  1. Which attribute sets where form data is sent?
  2. What is the default method?
  3. Which method should a login form use, and why?
  4. How do you make the response open in a new tab?
  5. Which two attributes are needed for a file upload form?
  6. What does novalidate do?

Practice Answers

  1. action
  2. get
  3. post, so the password isn't shown in the URL or saved in history.
  4. target="_blank"
  5. method="post" and enctype="multipart/form-data".
  6. It turns off the browser's built-in validation.

Key Takeaways

  • action = where; method = how (get for searches, post for private data).
  • target chooses where the response opens.
  • autocomplete helps visitors; novalidate skips browser checks.
  • File uploads need post + enctype="multipart/form-data".