The enctype attribute in HTML specifies how form data should be encoded before being sent to the server. It is mainly used with the <form> tag when submitting form data using the POST method.
- Defines the encoding type for form submission.
- Common values include application/x-www-form-urlencoded, multipart/form-data, and text/plain.
- Required when uploading files using <input type="file">.
Syntax:
<form enctype="multipart/form-data">
<!-- form elements -->
</form>
Attribute Values
This attribute contains three values which are listed below:
- application/x-www-form-urlencoded: Default encoding type that encodes form data before submission.
- multipart/form-data: Used for file uploads without encoding characters.
- text/plain: Sends form data in plain text format.
Supported Tag
The enctype attribute is associated with <form> element only.
Example: 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.
<!--Driver Code Starts-->
<!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>
<!--Driver Code Ends-->
<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>
<!--Driver Code Starts-->
</body>
</html>
<!--Driver Code Ends-->