Open In App

HTML formmethod Attribute

Last Updated : 16 Dec, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

The HTML formmethod Attribute is used to define the HTTP method that is used to send form data while submitting the form. GET and POST are the two well-known HTTP methods. This attribute overrides the feature of the method attribute of the <form> element. 

Supported tags: 

Syntax:

<element formmethod="get|post">

Values:

  • get: It is a default value. In the GET method, after the submission of the form, the form values will be visible in the address bar of the new browser tab. It has a limited size of about 3000 characters. It is only useful for non-secure data but not for sensitive information.
  • post: In the post method, after the submission of the form, the form values will not be visible in the address bar of the new browser tab as it was visible in the GET method. It appends form data inside the body of the HTTP request. It has no size limitation. This method does not support bookmarking of the result.

Example 1: Below code demonstrates the use of formmethod attribute in the HTML <input type=”submit”> control. Refer to the output given below for a better understanding. Notice the address bar of the new web page on clicking “Submit normally” and “submit using POST method”. Both are different, one showing the user details and the latter hiding all the user details.

HTML




<!DOCTYPE html>
<html>
 
<head>
    <title>
        HTML formmethod Attribute
    </title>
</head>
 
<body style="text-align:center;">
 
    <h1 style="color:green;">
        GeeksforGeeks
    </h1>
 
    <h3>HTML formmethod Attribute</h3>
    <form action="#"
        id="users"
        action="#"
        method="GET"
        target="_blank">
 
        User_id:
        <input type="email"
            name="email"
            placeholder="Enter Email Id">
 
        <br>
        <br> Password:
        <input type="password"
            name="pword"
            placeholder="Enter Password">
        <br>
        <br>
 
        <input type="submit" value="Submit normally">
       
      <input type="submit" formaction="#"
             value="submit using POST method"
             formmethod="post">
 
    </form>
</body>
 
</html>


Output:

Example 2: The following example demonstrates the HTML formmethod=”get” attribute using the button element. It submits the user entries to the “getDetail.php” PHP file. When the user clicks “Submit using get method”, the values are submitted to the PHP file, notice the address bar in the below output.

HTML




<!DOCTYPE html>
<html>
 
<body style="text-align:center;">
 
    <h1 style="color:green;">
        GeeksforGeeks
    </h1>
 
    <h3>HTML formmethod get Attribute</h3>
    <form action="getDetail.php">
 
        User name:
        <input name="username"
            placeholder="Enter username">
 
        <br>
        <br> Profession:
        <input
            name="profession"
            placeholder="Enter profession">
        <br>
        <br>   
       
      <button formmethod="get">            
             Submit using get method
      </button>
 
    </form>
</body>
 
</html>


Output:        

Supported Browsers:

  • Google Chrome 9.0
  • Internet Explorer 10.0
  • Firefox 4.0
  • Safari 5.1
  • Opera 10.6


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads