Form Attributes

Form attributes in HTML are additional properties that can be applied to HTML form elements. These attributes provide essential information about how the form should behave, how data should be submitted, and more.

Understanding and using form attributes is crucial for creating interactive and user-friendly web forms. These attributes control various aspects of form behavior and data submission.

Basic Form Attributes

action Attribute

The action attribute specifies the URL where the form data is sent after submission.

				
					<form action="https://example.com/submit">

				
			

In this example, once the user submits the form, the data will be sent to the specified URL.

Example

				
					<form action="/submit_form">
  <label for="name">Name:</label>
  <input type="text" id="name" name="user_name">
  <input type="submit" value="Submit">
</form>

				
			

Explanation

  • The form’s action is set to /submit_form.
  • When the user submits the form, the data will be sent to the /submit_form endpoint.

method Attribute

The method attribute specifies how to send the form data. It accepts two values:

  • GET (default): Data is appended to the URL.
  • POST: Data is sent as a separate HTTP request body.

Syntax

				
					<form action="/submit" method="post">

				
			

Example

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

				
			

Explanation

  • In this example, the form uses the POST method to send data to /submit.
  • The POST method is preferred for sensitive information, such as passwords, since data is not exposed in the URL.

target Attribute

The target attribute defines where the response should be displayed after form submission. It can have values like _blank, _self, _parent, or _top.

Syntax

				
					<form action="/submit" target="_blank">

				
			

Example

				
					<form action="/submit" target="_blank">
  <label for="username">Username:</label>
  <input type="text" id="username" name="username">
  <input type="submit" value="Submit">
</form>

				
			

Explanation

  • When the user submits the form, a new browser tab or window opens (due to _blank) and displays the response from the server.

enctype Attribute

The enctype attribute specifies how form data should be encoded when sending it to the server. It is only used with the POST method. The most common encoding types are:

  • application/x-www-form-urlencoded (default)
  • multipart/form-data: Used when uploading files.
  • text/plain

Syntax

				
					<form action="/upload" method="post" enctype="multipart/form-data">

				
			

Example

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

				
			

Explanation

  • The form uses enctype="multipart/form-data", which is necessary when uploading files to ensure they are transmitted correctly.

autocomplete Attribute

The autocomplete attribute controls whether the browser should automatically fill in values based on previously entered data.

Syntax

				
					<form action="/submit" autocomplete="on">

				
			

Example

				
					<form action="/submit" autocomplete="off">
  <label for="address">Address:</label>
  <input type="text" id="address" name="address">
  <input type="submit" value="Submit">
</form>

				
			

Explanation

  • The autocomplete="off" disables automatic filling of data by the browser for all input fields within the form.

novalidate Attribute

The novalidate attribute prevents the browser from validating form inputs before submission. This is useful when the developer wants custom validation instead of HTML5 built-in validation.

Syntax

				
					<form action="/submit" novalidate>

				
			

Example

				
					<form action="/submit" novalidate>
  <label for="age">Age:</label>
  <input type="number" id="age" name="age" min="18" max="100">
  <input type="submit" value="Submit">
</form>

				
			

Explanation

  • Even though the input element has a minimum and maximum value, the form will not perform validation since novalidate is used.

name Attribute

The name attribute is used to reference form data after submission. It is crucial when collecting and processing data on the server.

Syntax

				
					<form name="contactForm">

				
			

Example

				
					<form action="/submit" name="contactForm">
  <label for="phone">Phone:</label>
  <input type="tel" id="phone" name="user_phone">
  <input type="submit" value="Submit">
</form>

				
			

Explanation

  • The name attribute gives an identifier to the form, which can be accessed by client-side scripts or the server.

accept-charset Attribute

The accept-charset attribute specifies the character encoding used for form submission. By default, forms use UTF-8.

Syntax

				
					<form action="/submit" accept-charset="UTF-8">

				
			

Example

				
					<form action="/submit" accept-charset="ISO-8859-1">
  <label for="message">Message:</label>
  <input type="text" id="message" name="user_message">
  <input type="submit" value="Submit">
</form>

				
			

Explanation

  • The form uses ISO-8859-1 encoding, which is useful when dealing with different languages and character sets.

onsubmit and onreset Attributes

The onsubmit attribute executes JavaScript code when the form is submitted, while onreset triggers code when the form is reset.

Syntax

				
					<form action="/submit" onsubmit="return validateForm()">

				
			

Example

				
					<form action="/submit" onsubmit="return confirm('Are you sure?')">
  <label for="email">Email:</label>
  <input type="email" id="email" name="user_email">
  <input type="submit" value="Submit">
</form>

				
			

Explanation

  • When the user submits the form, a confirmation dialog will pop up asking, “Are you sure?” If the user clicks “OK”, the form will submit; otherwise, it will not.

Form attributes in HTML provide developers with a powerful way to control how data is collected, validated, and submitted. From basic attributes like action and method to more advanced attributes like enctype and onsubmit, understanding these properties allows developers to create robust, user-friendly forms.Happy coding !❤️

Table of Contents