search
  • Sign In
  • Sign Up
Password reset successful

Follow the proiects vou are interested in andi aet the latestnews about them taster

Table of Contents
1. Default Form Method
2. Overriding with formmethod
3. Key Points About formmethod
4. Practical Use Case
Home Web Front-end H5 Tutorial How does the formmethod attribute override the form's method?

How does the formmethod attribute override the form's method?

Aug 08, 2025 am 07:52 AM
html form

The formmethod attribute overrides the form's method for a specific submit button. 2. It allows different buttons in the same form to use different HTTP methods like get or post. 3. For example, one button can use POST to submit data while another uses GET to preview or search. 4. This override applies only when the button is clicked and does not affect other submissions. 5. It works alongside other form override attributes like formaction, formenctype, and formtarget. 6. Browser support is wide and it enables clean, JavaScript-free multi-behavior forms. The formmethod attribute provides per-button control over the HTTP method, overriding the form’s default method for that submission only, and is particularly useful for actions like submitting versus searching or previewing within the same form.

How does the formmethod attribute override the form\'s method?

The formmethod attribute is used on a submit button (like <input type="submit"> or <button></button>) to override the method attribute of the <form></form> element it belongs to. This allows different submit buttons within the same form to send data using different HTTP methods—most commonly get or post—without needing multiple forms.

How does the formmethod attribute override the form's method?

Here’s how it works:

1. Default Form Method

If a form has a method attribute specified (e.g., method="post"), all submit buttons in that form will use that method unless they explicitly set a formmethod.

How does the formmethod attribute override the form's method?
<form action="/submit" method="post">
  <input type="text" name="message">
  <button type="submit">Submit (uses POST)</button>
</form>

This will submit using POST.


2. Overriding with formmethod

When a submit button includes the formmethod attribute, it temporarily overrides the form’s method only for that submission.

How does the formmethod attribute override the form's method?
<form action="/submit" method="post">
  <input type="text" name="message">
  <button type="submit">Submit (POST)</button>
  <button type="submit" formmethod="get">Preview (GET)</button>
</form>

In this example:

  • Clicking the first button sends the data via POST.
  • Clicking the second button sends the data via GET, even though the form defaults to POST.

When the formmethod="get" button is clicked:

  • The form data is appended to the URL as query parameters (e.g., /submit?message=hello).
  • The HTTP method changes to GET, ignoring the original method="post".

3. Key Points About formmethod

  • Scope: Only applies to the specific button that has the formmethod attribute.
  • Flexibility: Useful for actions like "Save Draft" (POST) vs. "Preview" (GET) in the same form.
  • Other form attributes can also be overridden, like formaction, formenctype, and formtarget, giving per-button control over submission behavior.
  • Browser support: Widely supported in all modern browsers.

4. Practical Use Case

Imagine a comment form where you want:

  • A Post Comment button (POST)
  • A Search Similar Comments button (GET)

You can do this without JavaScript:

<form action="/post-comment" method="post">
  <input type="text" name="comment">
  <button type="submit">Post Comment</button>
  <button type="submit" formmethod="get" formaction="/search-comments">Search</button>
</form>

Now:

  • "Post Comment" submits to /post-comment using POST.
  • "Search" submits to /search-comments?comment=... using GET.

So, formmethod gives you per-button control over the HTTP method, overriding the form's default method just for that submission. It’s a simple, clean way to add multiple behaviors to a single form. Basically, it’s a one-time override that applies only when that specific button is clicked.

The above is the detailed content of How does the formmethod attribute override the form's method?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

ArtGPT

ArtGPT

AI image generator for creative art from text prompts.

Stock Market GPT

Stock Market GPT

AI powered investment research for smarter decisions

Popular tool

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to get HTML form data as text and send to html2pdf? How to get HTML form data as text and send to html2pdf? Sep 06, 2023 pm 12:21 PM

html2pdf is a JavaScript package that allows developers to convert html to canvas, pdf, images, and more. It takes html as parameter and adds it to pdf or desired document. Additionally, it allows users to download the document after adding html content. Here we will access the form and add it to the pdf using the html2pdfnpm package. We will see different examples to add form data to pdf. Syntax User can follow the following syntax to pass html form data as text and send it to html2pdf. varelement=document.getElementById('form');html2

How to allow multiple file uploads in HTML form How to allow multiple file uploads in HTML form Aug 28, 2023 pm 08:25 PM

In this article, we will learn how to allow multiple files uploads in HTML forms. We use multiple attributes to allow multiple file uploads in HTML forms. Several properties are available for email and file input types. Ifyouwanttoallowausertouploadthefiletoyourwebsite,youneedtouseafileuploadbox,alsoknownasafile,selectbox.Thisiscreatedusingthe&lt;in

PHP file upload tutorial: How to upload files using HTML forms PHP file upload tutorial: How to upload files using HTML forms Jun 11, 2023 am 08:10 AM

PHP file upload tutorial: How to use HTML forms to upload files In the process of website development, the file upload function is a very common requirement. As a popular server scripting language, PHP can implement the file upload function very well. This article will introduce in detail how to use HTML forms to complete file uploads. 1. HTML form First, we need to use an HTML form to create a file upload page. In the HTML form, the enctype attribute needs to be set to "multipart/form-

How to process HTML forms using Java? How to process HTML forms using Java? Aug 10, 2023 pm 02:05 PM

How to handle HTML forms using Java? HTML form is one of the commonly used interactive elements in web pages, through which users can input and submit data. Java, as a powerful programming language, can be used to process and validate HTML form data. This article will introduce how to use Java to process HTML forms, with code examples. The basic steps for processing HTML form data in Java are as follows: monitor and receive POST requests from HTML forms; parse the parameters of the request; process data according to needs

PHP regular expression: how to match all form tags in HTML PHP regular expression: how to match all form tags in HTML Jun 23, 2023 am 10:38 AM

In web development, it is often necessary to use regular expressions to match strings. In HTML, the form tag is a very important tag, so if we need to get all the form tags in the page, then regular expressions become a very useful tool. This article will introduce using regular expressions in PHP to match all form tags in HTML. 1. The form tag in HTML The form tag is a very important tag in HTML. It is used to create forms. The form is used

What is the method attribute, and how do I use it to specify the HTTP method (GET or POST) used to submit the form? What is the method attribute, and how do I use it to specify the HTTP method (GET or POST) used to submit the form? Jun 24, 2025 am 12:55 AM

ThemethodattributeinHTMLformsdetermineshowdataissenttotheserver,usingeitherGETorPOST.GETappendsdatatotheURL,haslengthlimits,andissuitablefornon-sensitiverequestslikesearches.POSTsendsdatainthebody,offersbettersecurity,andisidealforsensitiveorlargedat

Best practices for structuring complex HTML forms. Best practices for structuring complex HTML forms. Jul 03, 2025 am 02:33 AM

The key to designing complex HTML forms lies in content organization rather than encoding. To improve user experience and reduce error rates, the following steps must be followed: 1. Use and divide logical blocks to enhance structural clarity, accessibility and maintenance; 2. Clear the binding relationship between controls and tags, and ensure that each input box has corresponding tags through for and id; 3. Use hidden fields and conditions to display reasonably, combine CSS/JS to control the display status and process data validity; 4. Design hierarchical error prompts to avoid relying on color only to distinguish. It is recommended to add icons, highlight borders and set summary areas.

What are the various input types available in html forms and their uses? What are the various input types available in html forms and their uses? Jul 04, 2025 am 03:06 AM

HTML forms support multiple input types to suit different data entry requirements. 1. Text input is used for basic data entry, such as text and password, and interaction can be enhanced through placeholder, maxlength, required and other attributes; 2. HTML5 introduces special input types such as email, number, date, tel, and url to improve data accuracy and usability; 3. Selection and operation controls include checkbox, radio, submit, and button, for multiple selection, single selection and submission operations; 4. Hidden is used to pass uneditable data, and file is used for file upload and can limit file types. When using it, you need to consider browsing

Related articles