Open In App

HTML enctype Attribute

Last Updated : 28 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

HTML enctype Attribute specifies that data will be present in the form and should be encoded when submitting to the server. This type of attribute can be used only if method = "POST".

Syntax 

<form enctype = "value">

Attribute Values

This attribute contains three values which are listed below:

Attribute Values

Descriptions

application/x-www-form-urlencoded

It is the default value. It encodes all the characters before sent to the server. It converts spaces into + symbols and special character into its hex value.

multipart/form-data

This value does not encode any character.

text/plain

This value convert spaces into + symbols but special characters are not converted.

Supported Tag

The enctype attribute is associated with <form> element only. 

Example:  In this example we demonstrates the enctype="multipart/form-data" attribute, used for file uploads. It includes fields for first name, last name, and address, and a submit button.

html
<!DOCTYPE html>
<html>

<head>
    <title>enctype attribute</title>
    <style>
        h1 {
            color: green;
        }

        body {
            text-align: center;
        }
    </style>
</head>

<body>
    <h1>GeeksforGeeks</h1>
    <h2>enctype Attribute</h2>
    <form action="#"
          method="post"
          enctype="multipart/form-data">

        First name: <input type="text" name="fname"><br>
        Last name: <input type="text" name="lname"><br>
        Address: <input type="text" name="Address"><br>

        <input type="submit" value="Submit">
    </form>
</body>

</html>

Output: 
 

Supported Browsers

The browser supported by enctype Attribute are listed below: 

HTML enctype Attribute - FAQs

When should you use enctype="multipart/form-data"?

Use enctype="multipart/form-data" when your form includes file uploads (e.g., using <input type="file">). This encoding type is necessary for the server to properly handle the binary data of files.

What is the default value of the enctype attribute?

The default value of the enctype attribute is application/x-www-form-urlencoded. This is used when the enctype attribute is not explicitly specified.

How does the enctype attribute affect form submission?

The enctype attribute affects how the browser encodes the form data and how it is sent to the server. The server must be configured to handle the specified encoding type to correctly process the data.

How does the enctype attribute impact file uploads?

For file uploads to work correctly, the form's enctype attribute must be set to multipart/form-data. This ensures that the binary file data is sent to the server in a format it can handle.

Is the enctype attribute required for all forms?

The enctype attribute is not required for all forms. It is optional, but it is necessary to use it with the correct value (multipart/form-data) when dealing with file uploads or other types of data that require special encoding.
 


Next Article

Similar Reads