Why is this random HTML button submitting my form…?

Good question.  And the very same one that I had.

Here’s something new I learned:

If I have a button anywhere on the same page as my form, then that button decides that it wants to submit that form.  Even if I have a submit button within the form.

The solution

Put type=”button” on the button.  Then the browser knows that it’s a button:

<button type="button">Random button somewhere on the page</button>

You’d think that the fact that the element is called ‘button’ would be enough of a clue.

3 much appreciated responses...

  1. Abbas says:

    Thank you so much. Saved me from unnecessary worry. Such a simple thing and yet I didn’t know 😉

    • John B says:

      Haha yep, took me a while and a few cups of coffee to figure it out!

  2. Matt says:

    Wow… I owe you a beer sometime. That was suppppper annoying. What engineer thought to himself: “Hey lets make any random button element, connect to any random form element, and connect the submit action to it”

Feel free to add your comment..

Your email address will not be published.