HTML Form Attributes: Complete Guide with Examples

Learn HTML form attributes such as action, method, enctype, target, autocomplete, and novalidate with simple examples and practical uses.


HTML Form Attributes: A Complete Guide with Examples

HTML form attributes control how a form collects, validates, and submits information to a server. They help developers specify where form data should go, which HTTP method should be used, whether fields must be completed, and how submitted data should be encoded.

Understanding HTML form attributes is essential for creating registration forms, login pages, contact forms, search forms, feedback forms, and online application forms.

What Are HTML Form Attributes?

HTML form attributes are special properties written inside the opening <form> tag. They define the behavior of the form when a user enters information and submits it.

Example:

<form action="/submit" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required>
<button type="submit">Submit</button>
</form>

In this example:

  • action specifies where the form data is submitted.
  • method determines how the data is sent.
  • required makes the input mandatory.

The <form> element supports several attributes, each serving a different purpose.

Important HTML Form Attributes

The following table explains the most important attributes used with the HTML <form> element.

AttributePurpose
actionSpecifies the URL that processes the submitted form data.
methodDefines the HTTP method used to submit the data.
enctypeSpecifies how form data is encoded when submitted using POST.
targetDetermines where the server’s response is displayed.
autocompleteControls whether the browser can suggest previously entered information.
novalidateDisables the browser’s built-in form validation during submission.
accept-charsetSpecifies the character encoding used for submission.
nameAssigns a name to the form for identification and scripting.
relSpecifies the relationship between the current page and the destination resource, where applicable.

Let’s understand each attribute with practical examples.

1. The action Attribute

The action attribute specifies the URL or endpoint that receives and processes the form data after submission.

Example:

<form action="/submit-form" method="post">
<label for="email">Email:</label>
<input type="email" id="email" name="email">
<button type="submit">Send</button>
</form>

When the user submits the form, the browser sends the data to /submit-form.

Important points:

  • The value can be a relative URL or an absolute URL.
  • If action is omitted or empty, the form submits to the current document URL.
  • The destination must be configured to handle the submitted data.
  • The formaction attribute on a submit button can override the form’s action for that submission.

2. The method Attribute

The method attribute determines the HTTP method used to send form data.

The two most commonly used values are get and post.

GET method

The GET method sends form data as URL parameters.

<form action="/search" method="get">
<input type="search" name="query" placeholder="Search here">
<button type="submit">Search</button>
</form>

If the user searches for HTML, the resulting URL may look like this:

/search?query=HTML

Common uses: Search forms, filters, and other requests that retrieve information without changing server-side data.

POST method

The POST method sends form data in the request body rather than appending it to the URL.

<form action="/register" method="post">
<input type="text" name="username" placeholder="Username" required>
<input type="email" name="email" placeholder="Email" required>
<button type="submit">Register</button>
</form>

Common uses: Registration, contact forms, and operations that submit or change information.

Remember that POST does not automatically encrypt data. Use HTTPS to protect information in transit, and implement appropriate server-side validation and security controls.

3. The enctype Attribute

The enctype attribute defines how form data is encoded when the form uses method="post".

It has three commonly documented values.

ValueDescription
application/x-www-form-urlencodedThe default encoding for ordinary form submissions.
multipart/form-dataSends data in separate parts and is required for standard file-upload forms.
text/plainSends data as plain text with minimal encoding; generally unsuitable for production form processing.

Example: File upload

<form action="/upload" method="post"
enctype="multipart/form-data">
<label for="file">Choose a file:</label>
<input type="file" id="file" name="document">
<button type="submit">Upload</button>
</form>

Use multipart/form-data when users need to upload files. The server must also support receiving and securely processing those files.

4. The target Attribute

The target attribute determines where the response to the submitted form is displayed.

ValueMeaning
_selfDisplays the response in the current browsing context.
_blankOpens the response in a new tab or window, depending on the browser.
_parentDisplays the response in the parent browsing context.
_topDisplays the response in the top-level browsing context.

Example:

<form action="/results" method="get" target="_blank">
<input type="text" name="query" placeholder="Enter a search term">
<button type="submit">Search</button>
</form>

Here, the browser attempts to display the server’s response in a new tab or window.

The default behavior is equivalent to _self.

5. The autocomplete Attribute

The autocomplete attribute controls whether the browser may automatically complete form fields using previously entered or saved information.

It accepts on and off as form-level values.

Example:

<form action="/login" method="post" autocomplete="on">
<label for="email">Email:</label>
<input type="email" id="email" name="email">
<button type="submit">Continue</button>
</form>

When autocomplete is enabled, the browser may suggest saved information.

For more precise control, individual input fields can use values such as name, email, username, and current-password.

Note that browsers and password managers may handle autocomplete preferences differently, especially for login fields.

6. The novalidate Attribute

The novalidate attribute tells the browser not to perform its usual built-in form validation when the form is submitted.

Example:

<form action="/submit" method="post" novalidate>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required>
<button type="submit">Submit</button>
</form>

Although the email field is marked required, the browser will not block submission through its normal interactive form-validation process because novalidate is present.

Important: Disabling browser validation does not remove the need for server-side validation. Never rely solely on HTML validation to protect an application.

A submit button can also use formnovalidate to disable validation for just that submission.

7. The accept-charset Attribute

The accept-charset attribute specifies the character encoding used for form submission.

In modern HTML, UTF-8 is the standard and the only valid value for this attribute.

Example:

<form action="/submit" method="post" accept-charset="UTF-8">
<label for="name">Your name:</label>
<input type="text" id="name" name="name">
<button type="submit">Submit</button>
</form>

UTF-8 supports a wide range of characters, including English letters, Indian languages, accented characters, and many other writing systems.

For most modern websites, UTF-8 is the appropriate choice.

8. The name Attribute

The name attribute gives the form a name that can be used by scripts and other browser interfaces.

Example:

<form name="contactForm" action="/contact" method="post">
<input type="text" name="username">
<button type="submit">Send</button>
</form>

In this example:

  • contactForm identifies the form.
  • username identifies a field in the submitted data.

The form’s name attribute is different from an input’s name attribute. The latter is particularly important because it identifies the field’s value in the submitted form data.

A form does not need a name attribute if it can be accessed through other methods, such as an element ID or JavaScript references.

9. The rel Attribute

The rel attribute can describe the relationship between the current document and the resource specified by the form’s destination.

It is not commonly needed for ordinary forms. It can be relevant in specific browsing and security scenarios involving form submissions.

For everyday contact, login, and registration forms, developers generally focus on action, method, enctype, and validation-related attributes instead.

Other Attributes That Affect Form Submission

Some important form-related attributes belong to submit buttons or individual input elements rather than directly to the <form> element.

AttributeApplied toPurpose
formactionSubmit button or input submit controlOverrides the form’s destination for that submission.
formmethodSubmit button or input submit controlOverrides the form’s HTTP method.
formenctypeSubmit button or input submit controlOverrides the form’s encoding type.
formtargetSubmit button or input submit controlOverrides the response browsing target.
formnovalidateSubmit button or input submit controlBypasses normal interactive validation for that submission.
requiredSupported form controlsRequires a value before normal validated submission.
disabledSupported form controlsDisables a control and excludes its value from ordinary form submission.
readonlySupported text-entry controlsPrevents the user from editing the value while generally allowing it to be submitted.

Example: formaction and formmethod

<form action="/save" method="post">
<input type="text" name="message" placeholder="Enter a message">
<button type="submit">Save</button>
<button type="submit"
formaction="/preview"
formmethod="get">
Preview
</button>
</form>

The Save button submits to /save using POST. The Preview button overrides the destination and method, submitting to /preview using GET.

This is useful when one form provides multiple actions.

Complete Example of HTML Form Attributes

The following example combines several important attributes into a practical contact form.

<form action="/contact"
method="post"
autocomplete="on"
accept-charset="UTF-8">
<label for="fullname">Full Name:</label>
<input type="text"
id="fullname"
name="fullname"
autocomplete="name"
required>
<br><br>
<label for="email">Email Address:</label>
<input type="email"
id="email"
name="email"
autocomplete="email"
required>
<br><br>
<label for="message">Message:</label>
<textarea id="message"
name="message"
rows="5"
required></textarea>
<br><br>
<button type="submit">Send Message</button>
</form>

How this form works

  1. action="/contact" specifies the server endpoint.
  2. method="post" sends the submitted fields in the request body.
  3. autocomplete="on" permits browser autofill.
  4. accept-charset="UTF-8" specifies UTF-8 character encoding.
  5. required prevents ordinary validated submission when a required field is empty.
  6. Each field has a name attribute so its value can be included in the submitted data.

The /contact endpoint must be implemented on the server to receive and process the information.

Best Practices for Using HTML Form Attributes

Follow these practices when developing forms:

  • Choose the correct method: Use GET for searches and retrieval; use POST when submitting information that changes data or should not appear in the URL.
  • Use HTTPS: Protect data in transit, particularly personal and login information.
  • Add labels: Connect each input to a descriptive <label> to improve usability and accessibility.
  • Validate on the server: Browser-side validation can be bypassed, so validate all submitted data on the server.
  • Use required carefully: Make only genuinely necessary fields mandatory.
  • Choose the correct encoding: Use multipart/form-data for file uploads.
  • Provide meaningful names: Use clear field names such as email, username, and phone.
  • Avoid unnecessary attributes: Keep the form simple and include attributes that serve a clear purpose.
  • Handle errors clearly: Tell users how to correct invalid or missing information.
  • Protect uploaded files: Validate file types, sizes, and contents on the server.

Conclusion

HTML form attributes define how web forms behave, submit information, and interact with browsers and servers. The most important attributes to learn first are action, method, enctype, target, autocomplete, novalidate, accept-charset, and name.

By combining these attributes with appropriate input types, labels, validation, and secure server-side processing, developers can create useful, accessible, and reliable web forms.

Frequently Asked Questions (FAQs)

1. What are HTML form attributes?

HTML form attributes are properties added to the <form> element to control how a form submits data, validates information, and interacts with the browser.

2. What is the action attribute in HTML?

The action attribute specifies the URL or endpoint where the browser sends form data when the user submits the form.

3. What is the purpose of the method attribute?

The method attribute specifies the HTTP method used to submit form data. The most common values are get and post.

4. What is the difference between GET and POST?

GET typically places form data in the URL, making it suitable for searches and filters. POST sends data in the request body and is commonly used for registration and other data-submission operations. POST alone does not encrypt information.

5. What does the enctype attribute do?

The enctype attribute specifies how form data is encoded during a POST submission. The multipart/form-data value is required for standard HTML file uploads.

6. What is the target attribute in an HTML form?

The target attribute specifies where the response to a form submission is displayed, such as the current tab or a new tab.

7. What is the autocomplete attribute?

The autocomplete attribute controls whether the browser can suggest previously saved information for form fields, such as names, email addresses, and usernames.

8. What does the novalidate attribute mean?

The novalidate attribute disables the browser’s normal interactive form validation when the form is submitted. Server-side validation is still necessary.

9. What is the accept-charset attribute?

The accept-charset attribute specifies the character encoding used for form submission. UTF-8 is the standard choice for modern HTML forms.

10. What is the name attribute in an HTML form?

The name attribute identifies a form for browser interfaces and scripting. Input elements also use their own name attributes to identify submitted field values.

11. Can an HTML form have multiple submit buttons?

Yes. A form can contain multiple submit buttons. Attributes such as formaction and formmethod allow individual buttons to override the form’s destination or submission method.

12. Which HTML form attributes are most important?

The most important attributes for beginners are action, method, enctype, target, autocomplete, novalidate, and accept-charset.


Discover more from DL Mendali

Subscribe now to keep reading and get access to the full archive.

Continue reading