In HTML Forms, the enctype attribute used to handle encoding of form submitted data. When we use form GET or POST action, we have to encode the form data to get form submitted data.
The HTML forms provide three methods for encoding. The default is application/x-www-form-urlencoded, which is more or less the same as a query string on the end of the URL. The multipart/form-data is a more complicated encoding but one which allows entire files to be included in the form data.
Also, read:
- Upload Multiple Images using jQuery, PHP & MySQL
- Reduce or Compress Image Size While Uploading in PHP
- Image Upload without Page Refresh with PHP and jQuery
- Amazon S3 File Upload using PHP
- Angular Multiple File Upload with PHP and MySQL
- Create Thumbnails While Uploading Images with PHP & jQuery
- Amazon S3 File Upload using JavaScript
So when we have a HTML form with input type file to upload files, then we need to pass multipart/form-data with form. In short, we can use it when we have an <input type=”file” />.
There are three form enctypeAttribute values to use with HTML form. We will explain each in details.
1. application/x-www-form-urlencoded
This is default enctype attribute value and there no need to pass attribute enctype with this value if needed default form data encoding. With this attribute value, all characters are encoded before sending. The spaces are encoded with + symbols and special characters are encoded to ASCII HEX values.
2. multipart/form-data
When we use enctype attribute value multipart/form-data, the characters are not encoded. The form data transmitted as a MIME stream. This enctype attribute value is used when we need to use form with input type file to upload files.
<form action=“action.php” method=“get” enctype=“multipart/form-data”> Name: <input type=“text” name=“u_name” /> Profile photo: <input type=“file” name=“profile_image” /> <input type=“submit” value=“Submit” /> </form>
3. text/plain
The enctype attribute value text/plain is used transmit form data with encoding. When we use this attribute value, only spaces are converted to + symbols and there no special characters encoded.
<form action=“action.php” method=“get” enctype=“text/plain”> Name: <input type=“text” name=“name” /> Phone: <input type=“number” name=“phone” /> <input type=“submit” value=“Submit” /> </form>
You may also like:
- Library Management System with PHP & MySQL
- Online Food Ordering System with PHP & MySQL
- Build Hotel Reservation System with PHP & MySQL
- Star Rating System with Ajax, PHP and MySQL
- Create Event Calendar with jQuery, PHP and MySQL
- Build Your Own CAPTCHA Script with PHP
- Convert Unix Timestamp To Readable Date Time in PHP
- Inventory Management System with Ajax, PHP & MySQL
- Create Live Editable Table with jQuery, PHP and MySQL
- Live Add Edit Delete datatables Records with Ajax, PHP and MySQL
- Stripe Payment Gateway Integration in PHP
- Export Data to Excel with PHP and MySQL
- Star Rating System with Ajax, PHP and MySQL
- Create Dynamic Bootstrap Tabs with PHP & MySQL
- How To Create Simple REST API in PHP