HTML Form Attributes
HTML Form Attributes
The <form> element in HTML supports several attributes that control how form data is sent and displayed. These attributes define behavior, appearance, and validation for forms.
Common Form Attributes
| Attribute | Description | Example |
|---|---|---|
action |
Specifies the URL where form data is sent | <form action="submit.php"> |
method |
Defines the HTTP method (GET or POST) |
<form method="post"> |
enctype |
Specifies encoding type for sending data (application/x-www-form-urlencoded, multipart/form-data for file upload) |
<form enctype="multipart/form-data"> |
name |
Assigns a name to the form | <form name="contactForm"> |
id |
Unique identifier for the form | <form id="form1"> |
target |
Specifies where to display the response (_self, _blank, _parent, _top) |
<form target="_blank"> |
autocomplete |
Enables/disables autofill of form fields (on / off) |
<form autocomplete="off"> |
novalidate |
Disables HTML5 form validation | <form novalidate> |
Example Code Using Attributes
Explanation
-
action="submit.php"→ Sends form data tosubmit.php. -
method="post"→ Data is sent securely in HTTP request body. -
enctype="multipart/form-data"→ Allows file uploads. -
id="regForm"→ Unique identifier for CSS or JS. -
autocomplete="on"→ Browser can auto-fill fields.
Key Points
-
Always set the
actionandmethodattributes for proper form submission. -
Use
enctype="multipart/form-data"when handling file uploads. -
autocompleteandnovalidatehelp improve user experience and form control.
