How to specify how form-data should be encoded when submitting to server ?
HTML form provides three methods of encoding.
- application/x-www-form-urlencoded
- multipart/form-data
- text/plain
1.application/x-www-form-urlencoded: This is the default method that is applied if nothing is specified. In this method spaces are converted to the ‘+‘ symbol and special characters are converted to ASCII HEX value, and all other characters remain the same.
Example:
HTML
<!DOCTYPE html> < html > < body > <!--create form to show the implementation of encode type = application/x-www-form-urlencoded --> enctype = "application/x-www-form-urlencoded" > < label for = "fname" >First name:</ label > < input type = "text" id = "fname" name = "fname" > < br >< br > < label for = "lname" >Last name:</ label > < input type = "text" id = "lname" name = "lname" > < br >< br > < input type = "submit" value = "Submit" > </ form > </ body > </ html > |
Output:
- Accepting form data from the user.
- Accepted the user data on the server.
2.multipart/form-data: In this, no characters are encoded. This value is required when a form has a file to upload.
Example:
HTML
<!DOCTYPE html> < html > < body > < h2 >Welcome To GFG</ h2 > <!-- create form to show the implementation of encode type = multipart/form-data --> enctype = "multipart/form-data" > < label for = "fname" >First name:</ label > < input type = "text" id = "fname" name = "fname" > < br >< br > < label for = "lname" >Last name:</ label > < input type = "text" id = "lname" name = "lname" > < br >< br > < input type = "submit" value = "Submit" > </ body > </ html > |
Output:
- Accepting form data from the user.
- Accepted the user data on the server.
3.text/plain: In this, spaces are converted to the ‘+‘ symbol, but no other characters are encoded.
Example:
HTML
<!DOCTYPE html> < html > < body > <!-- create form to show the implementation of encode type = text/plain --> enctype = "text/plain" > < label for = "fname" >First name:</ label > < input type = "text" id = "fname" name = "fname" > < br >< br > < label for = "lname" >Last name:</ label > < input type = "text" id = "lname" name = "lname" > < br >< br > < input type = "submit" value = "Submit" > </ body > </ html > |
Output:
- Accepting form data from the user.:
- Accepted the user data on the server.
Contact Us