HTML enctype Attribute

The enctype attribute is used to encode the information in the form before sending it to the server so that the information is not corrupted during the transmission. The encoding is performed by the browser.

Three types of encoding process are application/x-www-form-urlencoded, multipart/form-data, and text/plain.

The application/x-www-form-urlencoded encoding is the standard form of encoding. If no value is set for the enctype attribute, browser accepts the standard type.

The multipart/form-data encoding is required for those forms that contain file selection field.

You can use the text/plain attribute along with the mailto URL to send the form information to an e-mail address rather than a server.

The application/x-www-form-urlencoded Encoding

The application/x-www-form-urlencoded encoding acts as a default type of encoding for the browser. This implies that if the enctype attribute is not set with any value, the browser by default uses the application/x-www-form-urlencoded encoding.

In this encoding, the browser converts a space into a a plus sign (+), a non-alphanumeric character into a percent sign (%) followed by two hexadecimal digits that are ASCII code of character, and line breaks into CR/LF pairs (%0D%0A).

The standard encoding also contains the name of each form controls that is present in the form. Each control name is separated from other control name by an ampersand (&).

Example of application/x-www-form-urlencoded Encoding

Let's consider a form, which contains four controls named username, password, name, and address. The values of these controls are richjones, rich23, richard, and Sydney, Australia respectively. In such a case, the encoding of the information in the form is done in the following way :


The multipart/form-data Encoding

The multipart/form-data encoding is used only when the method attribute of the form is set to post. In this type of encoding, the controls in the form are segregated in several parts. This encoding is more cumbersome and longer than the application/x-www-form-urlencoded encoding.

Example of multipart/form-data Encoding

Let's consider the example used in the application/x-www-form-urlencoded encoding. The form contains four controls named username, password, name, and address, and their values are richjones, rich23, richard, and Sydney, Australia, respectively. The encoding of the given example in the multipart/form-data encoding is as follows:

Content-Disposition: form-data; name="username"

Content-Disposition: form-data; name="password"

Content-Disposition: form-data; name="name"

Content-Disposition: form-data; name="address"

Sydney, Australia

As already discussed this type is preferred only when the form contains one or more file selection control. Let's consider an example where the form contains a file selection control named thefile and the filename is file. In this case, the multipart/form-data encoding is given as follows:

Content-Disposition: form-data; name="thefile"; filename="file"

Content of the file...

The text/plain Encoding

The text/plain encoding is used when the browser does not have access to the server that contains the form-handling program. In this case, the information form the form is sent through an e-mail and the action attribute is set to a mailto URL. In this type of encoding, each control is written in a single line, with name and value separated by an equal to sign (=). Space is denoted by space and line break is denoted by CR/LF pairs (%0D%0A).

Example of text/plain Encoding

Let's again consider the example used in the application/x-www-form-urlencoded encoding in which, there are four controls named username, password, name, and address. The encoding of the information in the form is given as follows:

address=Sydney, Australia

HTML Online Test

« Previous Tutorial Next Tutorial »

Like/Share Us on Facebook 😋