HTML Submit formMethod Property: Submit Button Form Method

June 19, 2025

HTML Submit formMethod Property: Specifying the HTTP Method for Form Submission

The formMethod property of the HTML <input type="submit"> element allows you to override the method attribute of the <form> element. This lets you define a specific HTTP method (either GET or POST) that will be used when the submit button is clicked, providing greater control over how form data is sent to the server. This article delves into the details of the formMethod property, its syntax, usage with examples, and practical applications.

What is the formMethod Property?

The formMethod property specifies the HTTP method to use when submitting the form. It overrides the method attribute defined in the <form> tag, giving individual submit buttons the ability to use different methods. This is particularly useful in complex forms where certain submission actions might require a different method than the form’s default.

Purpose of the formMethod Property

The primary purpose of the formMethod property is to:

  • Override the form’s default submission method for specific submit buttons.
  • Allow different submit buttons within the same form to use different HTTP methods.
  • Provide flexibility in handling form submissions based on the specific action triggered by the submit button.

Syntax

The formMethod property is specified as an attribute of the <input type="submit"> element:

<input type="submit" value="Submit" formMethod="get|post">

Here:

  • formMethod is the property you are setting.
  • "get" or "post" are the possible values, specifying the HTTP method to use.

Possible Values

Value Description
`get` The form data is appended to the URL and sent to the server using the GET method. This is suitable for small amounts of data and non-sensitive information.
`post` The form data is sent to the server as part of the HTTP request body using the POST method. This is suitable for larger amounts of data and sensitive information.

Examples

Let’s explore some practical examples to illustrate how to use the formMethod property effectively.

Basic Usage: Overriding Form Method

In this example, we have a form with a default method of POST, but the submit button overrides this to use GET.

<form id="myFormMethodBasic" action="/submit" method="post">
  <label for="name">Name:</label><br />
  <input type="text" id="name" name="name" /><br /><br />
  <input type="submit" value="Submit using GET" formMethod="get" />
</form>

In this case, when the submit button is clicked, the form data will be submitted using the GET method, regardless of the form’s method attribute.

Using Different Methods for Multiple Submit Buttons

This example demonstrates how to use different formMethod values for multiple submit buttons within the same form.

<form id="myFormMethodMultiple" action="/submit">
  <label for="email">Email:</label><br />
  <input type="email" id="email" name="email" /><br /><br />
  <input type="submit" value="Submit using GET" formMethod="get" />
  <input type="submit" value="Submit using POST" formMethod="post" />
</form>

Here, one submit button uses the GET method, while the other uses the POST method. This is useful for actions like “Preview” (GET) and “Submit” (POST) within the same form.

Dynamic Form Method Using JavaScript

You can also dynamically set the formMethod property using JavaScript based on certain conditions.

<form id="myFormMethodDynamic" action="/submit" method="post">
  <label for="message">Message:</label><br />
  <textarea id="message" name="message"></textarea><br /><br />
  <input type="submit" value="Submit" id="submitButton" />
</form>

<script>
  const submitButtonMethod = document.getElementById("submitButton");

  submitButtonMethod.addEventListener("click", function (event) {
    const messageInput = document.getElementById("message");
    if (messageInput.value.length > 100) {
      submitButtonMethod.formMethod = "post";
    } else {
      submitButtonMethod.formMethod = "get";
    }
  });
</script>

In this example, the formMethod is dynamically set to POST if the message length is greater than 100 characters; otherwise, it is set to GET.

Real-World Applications of the formMethod Property

  1. Search Forms: Use GET for search forms to allow users to easily share or bookmark search queries.
  2. Data Submission Forms: Use POST for forms that submit sensitive or large amounts of data, such as registration or checkout forms.
  3. API Interactions: When interacting with APIs that require specific HTTP methods for different actions, the formMethod property can be used to direct the form submission to the appropriate endpoint.
  4. Conditional Data Handling: Dynamically change the submission method based on the user’s input or other conditions, as shown in the JavaScript example above.

Use Case Example: Implementing a Search and Submit Form

Let’s create a practical example that demonstrates how to use the formMethod property to implement a form with both search and submit functionalities. The search functionality will use GET to allow bookmarking, while the submit functionality will use POST for secure data submission.

<form id="searchSubmitForm" action="/process">
  <label for="query">Search:</label>
  <input type="text" id="query" name="query" />
  <input type="submit" value="Search" formMethod="get" /><br /><br />

  <label for="data">Data:</label>
  <input type="text" id="data" name="data" />
  <input type="submit" value="Submit" formMethod="post" />
</form>

This example incorporates:

  1. Clear Separation of Concerns: Distinguishes between search and submit actions using different HTTP methods.
  2. User Experience: Enhances user experience by allowing search queries to be bookmarkable.
  3. Data Security: Ensures secure data submission using the POST method.

Browser Support

The formMethod property is supported by all modern browsers.

Note: Always test your forms across different browsers to ensure consistent behavior. 🧐

Conclusion

The formMethod property of the HTML submit button is a powerful feature that provides flexibility in handling form submissions. By allowing you to override the form’s default method on a per-button basis, you can create more versatile and user-friendly web applications. Whether you’re building a simple search form or a complex data submission system, understanding and utilizing the formMethod property can greatly enhance the functionality and user experience of your web forms. Happy coding!