HTML Form Attributes Tutorial
An HTML form is used to collect user input. The form
element in HTML has several attributes that can be used to control the behavior of the form. In this tutorial, we will go over some of the most commonly used form attributes and how to use them.
1. action Attribute
The action
attribute specifies where the form data should be submitted when the user submits the form. It should be a URL where the form data will be sent to. If the action
attribute is not specified, the form data will be submitted to the current page.
<form action="submit_form.php" method="post">
<!-- form fields go here -->
</form>
2. method Attribute
The method
attribute specifies how the form data should be submitted. It can have two values: get
or post
. The get
method appends the form data to the URL, while the post
method sends the data in the request body.
<form action="submit_form.php" method="post">
<!-- form fields go here -->
</form>
3. target Attribute
The target
attribute specifies where the response from the form submission should be displayed. It can have values such as _self
, _blank
, _parent
, or _top
. If not specified, the response will be displayed in the same window.
<form action="submit_form.php" method="post" target="_blank">
<!-- form fields go here -->
</form>
4. autocomplete Attribute
The autocomplete
attribute specifies whether the browser should autocomplete form fields. It can have values of on
or off
.
<form action="submit_form.php" method="post" autocomplete="off">
<!-- form fields go here -->
</form>
5. enctype Attribute
The enctype
attribute specifies how the form data should be encoded before it is sent to the server. It is used when the form includes file uploads. The value should be set to multipart/form-data
when uploading files.
<form action="submit_form.php" method="post" enctype="multipart/form-data">
<!-- form fields go here -->
</form>
6. novalidate Attribute
The novalidate
attribute specifies that the form should not be validated when submitted. This can be useful when testing a form before it is ready for production.
<form action="submit_form.php" method="post" novalidate>
<!-- form fields go here -->
</form>
Attribute | Description |
---|---|
action | Specifies where the form data should be submitted |
method | Specifies how the form data should be submitted |
target | Specifies where the response should be displayed |
autocomplete | Specifies whether form field autocomplete is enabled |
enctype | Specifies how form data should be encoded for file uploads |
novalidate | Specifies that the form should not be validated |