Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML why do buttons in forms send data?

Tags:

html

forms

button

This is a very rudimentary question, but I am sure someone out there knows why. In HTML, when I make a button element by itself, and do not give it and onclick and no jQuery .click() the button will just do nothing. Perfect. But when I do this and but the button inside a <form> element, it tries to send GET data of all the form elements to the root address of my website? Why is it doing that? I didn't make it a submit button or even define a method or action on that form??

Thanks for the info in advance!

** EDIT **

This is what I did to fix the problem. For buttons inside the <form>, use:

<button type="button"></button>

And it will not do anything by default.

like image 671
jeffery_the_wind Avatar asked Mar 28 '12 13:03

jeffery_the_wind


2 Answers

As can be seen at the respective MDN entry, the default value for the type property of a button element is submit. So if you omit it or don't change it to button or reset, the default behaviour will kick in and the form gets submitted.

<form action="">
  <button type="button">Nothing will happen</button>
  <button>Form gets submitted</button>
</form>
like image 119
Sirko Avatar answered Sep 29 '22 09:09

Sirko


I didn't make it a submit button

<button> elements have a type attribute. The default value is submit. Set type="button" if you don't want it to submit a form.

or even define a method

method defaults to GET

or location on that form??

action defaults to the current URI.

like image 29
Quentin Avatar answered Sep 29 '22 07:09

Quentin