Open In App

How to specify how form-data should be encoded when submitting to server ?

Improve
Improve
Like Article
Like
Save
Share
Report

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  -->
  <form  action="https://www.geeksforgeeks.org/" method="post" 
         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.

                        

output

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     -->
  <form  action="https://www.geeksforgeeks.org/" method="post" 
         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             -->
  <form  action="https://www.geeksforgeeks.org/" method="post" 
         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.

                             

output



Last Updated : 12 May, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads