HTML forms have several attributes that provide additional functionality and customization options. Here are some commonly used attributes for HTML forms:
action
: Specifies the URL or server-side script that will handle the form submission. For example:action="/submit-form"
.method
: Defines the HTTP method to be used when submitting the form. The two most commonly used methods are:
GET
: Sends the form data as parameters in the URL. This method is suitable for retrieving data and has limitations on the amount of data that can be sent.POST
: Sends the form data in the body of the HTTP request. This method is suitable for sending sensitive or large amounts of data.
name
: Provides a unique name for the form. This attribute is used to identify the form when processing the data on the server-side.target
: Specifies where the response from the server should be displayed. The commonly used values are:
_blank
: Opens the response in a new window or tab._self
: Loads the response in the same window/tab as the form._parent
: Loads the response in the parent frame of the current frame, if the form is embedded within frames._top
: Loads the response in the full body of the window, if the form is embedded within frames.
enctype
: Specifies how the form data should be encoded and sent to the server. The two most common values are:
application/x-www-form-urlencoded
: This is the default value. The form data is URL-encoded and included in the body of the HTTP request.multipart/form-data
: Used when uploading files through the form. The data is sent in multiple parts.
autocomplete
: Specifies whether the form should have autocomplete enabled or disabled for input fields. The values can be:
on
: Enables autocomplete.off
: Disables autocomplete.
novalidate
: Prevents the browser from validating form inputs before submission. This attribute is useful when you want to handle validation on the server-side.
These are just a few examples of the attributes available for HTML forms. There are additional attributes and options that can be used to enhance form functionality, such as placeholder
, disabled
, readonly
, pattern
, and more. It’s important to refer to the HTML specification or documentation for a comprehensive list of form attributes and their usage.
<form action="/action_page.php">
<label for="fname">First name:</label><br>
<input type="text" id="fname" name="fname" value="John"><br>
<label for="lname">Last name:</label><br>
<input type="text" id="lname" name="lname" value="Doe"><br><br>
<input type="submit" value="Submit">
</form>
Here are examples of HTML form attributes: action
, target
, method
, autocomplete
, and novalidate
.
action
attribute:
Theaction
attribute specifies the URL or server-side script that will handle the form submission.
<form action="/submit-form" method="post">
<!-- form fields -->
</form>
In this example, when the form is submitted, the data will be sent to the server-side script located at /submit-form
for processing.
target
attribute:
Thetarget
attribute specifies where the response from the server should be displayed.
<form action="/submit-form" method="post" target="_blank">
<!-- form fields -->
</form>
In this example, when the form is submitted, the response from the server will open in a new window or tab.
method
attribute:
Themethod
attribute defines the HTTP method to be used when submitting the form.
<form action="/submit-form" method="get">
<!-- form fields -->
</form>
In this example, when the form is submitted, the data will be sent as parameters in the URL using the GET
method.
autocomplete
attribute:
Theautocomplete
attribute specifies whether the form should have autocomplete enabled or disabled.
<form action="/submit-form" method="post" autocomplete="off">
<!-- form fields -->
</form>
In this example, autocomplete is disabled for the form, which means that the browser will not suggest or remember previously entered values for the form fields.
novalidate
attribute:
Thenovalidate
attribute prevents the browser from validating form inputs before submission.
<form action="/submit-form" method="post" novalidate>
<!-- form fields -->
</form>
In this example, the browser will not perform validation on the form inputs before submitting the form. The validation can be handled on the server-side.
These examples demonstrate the usage of the action
, target
, method
, autocomplete
, and novalidate
attributes in HTML forms. Remember that the actual behavior and functionality may vary depending on the server-side implementation and the browser being used.
List of All <form> Attributes
Attribute | Description |
---|---|
accept-charset | Specifies the character encodings used for form submission |
action | Specifies where to send the form-data when a form is submitted |
autocomplete | Specifies whether a form should have autocomplete on or off |
enctype | Specifies how the form-data should be encoded when submitting it to the server (only for method=”post”) |
method | Specifies the HTTP method to use when sending form-data |
name | Specifies the name of the form |
novalidate | Specifies that the form should not be validated when submitted |
rel | Specifies the relationship between a linked resource and the current document |
target | Specifies where to display the response that is received after submitting the form |