Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

button in the form cause it be submitted automatically

has the HTML version changed lately (like from ie7 to IE8?) I notice the following change that cause me some troulbe - I am having a code that is similar to this:

<form method="POST" action="/admin/modify"> <input type="text"/> <button onclick="dosomething()">Press</button> </form> <script type="text/javascript"> function doSomething(){ // doesn't matter what actually } </script> 

What is weired to me in this code is that by pressing the button inside the form, all I want is to perform some javascript action but eventaully it causes the form to be submitted too, even when I am not willing to do it.
So - is it true? and if so how can I perform some java script actoin inside a from but prevent the form from being submitted automatically?

like image 454
Spiderman Avatar asked Mar 13 '10 21:03

Spiderman


People also ask

What causes form submit?

By default, a button has an innate type property of submit . When tapped, clicked, or activated inside a form, it will cause that form to submit (and trigger a corresponding submit event). let form = document.

Does button submit form?

submit : The button submits the form data to the server. This is the default if the attribute is not specified for buttons associated with a <form> , or if the attribute is an empty or invalid value.

How do you stop re submitting a form after clicking back button?

You can check if the user clicked the back button, disable form if true. Another way is by storing a cookie which you check on page load, if it exists you can disable the form.


1 Answers

According to W3schools, submit is the new default action for button elements in IE 8:

Always specify the type attribute for the button. The default type for Internet Explorer is "button", while in other browsers (and in the W3C specification) it is "submit".

So if you don't specify a type, the form will be submitted in all browsers, but not IE 7.

this should work:

<button type="button" onclick="dosomething()">Press</button> 
like image 77
Pekka Avatar answered Oct 14 '22 06:10

Pekka