Open In App

HTML formmethod Attribute

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:



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.




<!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.




<!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:


Article Tags :