HTML Tutorial - HTML Form Encoding








The enctype attribute specifies how the browser encodes and presents the data to the server.

There are three allowed values for this attribute.

  • application/x-www-form-urlencoded
    Default encoding.
    This encoding cannot be used to upload files to the server.
  • multipart/form-data
    This encoding is used to upload files to the server.
  • text/plain
    This encoding varies between browsers.

To understand how the different encodings work, we created the following form.

<!DOCTYPE HTML>
<html>
<body>
  <form method="post" action="http://example.com/form">
    <input name="fave" /> 
    <input name="name" />
    <button>Submit Vote</button>
  </form><!--   w w w.  ja v  a  2  s . com-->
</body>
</html>

Click to view the demo





application/x-www-form-urlencoded

If using application/x-www-form-urlencoded encoding
The name and value of each data item is encoded using the same scheme that is used to encode URLs. This is how the encoding is applied to the data in the example form:

fave=Apples&name=FiratName+LastName

Special characters are replaced with their HTML entity counterpart. The name of the data item and the value are separated by the equals sign (=) and data/value tuples are separated by the ampersand character (&).

multipart/form-data

The multipart/form-data encoding tends to be used only for uploading files. Here is how the data from the example form is encoded:

------WebKitFormBoundary2desQWER543CDFGF

Content-Disposition: form-data; name="fave" YourName
------WebKitFormBoundary2desQWER543CDFGF Content-Disposition: form-data; name="name" java2s.com
------WebKitFormBoundary2desQWER543CDFGF-- fave=Apple
name=java2s.com




text/plain

The mainstream browsers encode data in different ways for this encoding.

Google Chrome encodes data in the same way as for the application/x-www-form-urlencoded scheme, whereas Firefox encodes the data as follows:

fave=xml
name=java2s.com

Each data item is placed on a line, and special characters are not encoded.