HTML Form Attributes define the behavior and functionality of a form. They are used to specify where the form data should be sent, the encoding type, HTTP methods, and more.
Here are the most frequently used attributes for forms:
action
: Specifies the URL to which the form data should be sent.method
: Defines the HTTP method to use (e.g., GET or POST).enctype
: Specifies how form data is encoded (e.g., for file uploads).autocomplete
: Enables or disables autocomplete for form fields.target
: Specifies where to display the response (e.g., in a new tab).action
Attribute
The action
attribute specifies the URL where the form data should be submitted. By default, if this attribute is omitted, the form data is submitted to the same page that contains the form.
Syntax:
<form action="URL"> <input type="text" name="username"> <button type="submit">Submit</button> </form>
method
Attribute
The method
attribute defines the HTTP method used to send form data. The two commonly used methods are:
Syntax:
<form method="method_type"> <input type="text" name="email"> <button type="submit">Submit</button> </form>
enctype
Attribute
The enctype
attribute specifies how form data should be encoded when it is submitted to the server. It is primarily used with forms that include file uploads.
Common encoding types:
Syntax:
<form enctype="encoding_type"> <input type="file" name="file"> <button type="submit">Upload</button> </form>
autocomplete
Attribute
The autocomplete
attribute enables or disables the browser’s autocomplete feature for form fields.
Values:
Syntax:
<form autocomplete="on|off"> <input type="text" name="name"> <button type="submit">Submit</button> </form>
target
Attribute
The target
attribute specifies where to display the response after form submission.
Syntax:
<form target="_self|_blank|_parent|_top"> <input type="text" name="query"> <button type="submit">Search</button> </form>
action
attribute to specify the target URL.method
attribute to set GET or POST requests.enctype
, target
, and autocomplete
.action
attribute?
The action
attribute specifies the server-side script or URL that will handle the form data.
method
attribute?
You can use GET
or POST
. GET is used for retrieving data, while POST is used for submitting data securely.
enctype
attribute?
The enctype
attribute is required when your form includes file uploads.