265

What is the difference between HTML <input type='button' /> and <input type='submit' />?

Always Helping
  • 14,316
  • 4
  • 13
  • 29
bounav
  • 4,886
  • 4
  • 28
  • 33
  • 4
    See also **[input type=“submit” Vs button tag are they interchangeable?](http://stackoverflow.com/questions/7117639/input-type-submit-vs-button-tag-are-they-interchangeable)** – hippietrail Nov 23 '11 at 16:40
  • 2008 Rocks, was HTML 5.0 there ? – vikramvi Sep 20 '22 at 12:06

4 Answers4

259

<input type="button" /> buttons will not submit a form - they don't do anything by default. They're generally used in conjunction with JavaScript as part of an AJAX application.

<input type="submit"> buttons will submit the form they are in when the user clicks on them, unless you specify otherwise with JavaScript.

The first submit button of the form is also the one being clicked for implicit submission, f.e. by pressing enter in a text input.

Andy
  • 4,783
  • 2
  • 26
  • 51
  • 52
    Also browsers can capture the "Enter" keypress on a form and submit the form automatically if there is a submit button, but not otherwise. – Mr. Shiny and New 安宇 Nov 14 '08 at 14:49
  • 2
    They also do that if you have a type="image", which can be used to trigger a form-submission when clicked on. – jishi Nov 14 '08 at 14:53
  • 5
    Mr. Shiny and New: Forms can be submitted via the enter key without any buttons. It's enough to have focus on a text input, for instance. – Lasar Nov 14 '08 at 15:03
  • There's also an actual button element that can also be used to submit forms when clicked. http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.5 – Powerlord Nov 14 '08 at 15:05
  • 4
    You can use BUTTON elements, although (surprise surprise) there are a few issues with them when using Everyone's Favourite Browser (IE). Worth knowing about though. –  Nov 14 '08 at 15:27
  • 4
    This is obviously extremely old but I feel the need to give my 2 cents as I feel it is a large downfall of using button types... the form onsubmit event is NOT fired from javascript submissions, leading to potential maintenance nightmares. – mothmonsterman Nov 19 '10 at 17:19
  • Also note that type="submit" may submit even when there is NO form on the page. The submit goes to the current URL in such case. This might lead to racing conditions if used together with onclick="document.location=...". – Vilmantas Baranauskas Sep 28 '12 at 12:26
  • What's the mechanism of submitting information with the submit button? I found that even though Javascript is disable, the submit button will continue the action. Is it just a normal post method which is default behaviour of the submit button across all browsers? – Hoy Cheung Apr 18 '13 at 22:10
  • What's the default HTTP SUBMIT address for ``? Meaning, to which address does the SUBMIT go? – Kevin Meredith Jul 23 '13 at 19:11
  • @KevinMeredith: the current page url. And if there are several submit buttons in a form, the browser takes the first in DOM. – Dan Jan 08 '14 at 23:06
  • Source for this answer https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input – Adriano Jul 07 '14 at 08:20
22

A 'button' is just that, a button, to which you can add additional functionality using Javascript. A 'submit' input type has the default functionality of submitting the form it's placed in (though, of course, you can still add additional functionality using Javascript).

Aistina
  • 12,435
  • 13
  • 69
  • 89
  • 3
    A ` – Sam Dutton Oct 13 '20 at 17:35
5

It should be also mentioned that a named input of type="submit" will be also submitted together with the other form's named fields while a named input type="button" won't.

With other words, in the example below, the named input name=button1 WON'T get submitted while the named input name=submit1 WILL get submitted.

Sample HTML form (index.html):

<form action="checkout.php" method="POST">

  <!-- this won't get submitted despite being named -->
  <input type="button" name="button1" value="a button">

  <!-- this one does; so the input's TYPE is important! -->
  <input type="submit" name="submit1" value="a submit button">

</form>

The PHP script (checkout.php) that process the above form's action:

<?php var_dump($_POST); ?>

Test the above on your local machine by creating the two files in a folder named /tmp/test/ then running the built-in PHP web server from shell:

php -S localhost:3000 -t /tmp/test/

Open your browser at http://localhost:3000 and see for yourself.

One would wonder why would we need to submit a named button? It depends on the back-end script. For instance the WooCommerce WordPress plugin won't process a Checkout page posted unless the Place Order named button is submitted too. If you alter its type from submit to button then this button won't get submitted and thus the Checkout form would never get processed.

This is probably a small detail but you know, the devil is in the details.

Eugen Mihailescu
  • 3,553
  • 2
  • 32
  • 29
4

IE 8 actually uses the first button it encounters submit or button. Instead of easily indicating which is desired by making it a input type=submit the order on the page is actually significant.

Martin Murphy
  • 1,775
  • 2
  • 16
  • 24