Mirakurun Mirakurun - 18 days ago 7
HTML Question

Submit calls function two times

When I submit the form with Enter, it works without any problems. I can successfully log in. When I submit the form with a button, it logs in successfully on Firefox, but not on Chrome. The problem is, it repeats the function twice and sends a different hashed password. How can I make it to work on Chrome too?

Button:

<div id="submit" name="submit" value="login" class="ui fluid large pink submit button" onclick="submitForm();">Login</div>


Form:

<form id="form" autocomplete="off" class="ui large form" id = "form" name="form" method="post" action="php/verify.php" onsubmit="submitForm();">


I added
onsubmit="submitForm();"
to form, to call the function even when I submit the form with
Enter
.

Javascript function:

function submitForm(){
var form = document.getElementById("form");
var pwd = document.getElementById('pwd');
var hash = new jsSHA("SHA-256", "TEXT", {numRounds: 1});
hash.update(pwd.value);
var hash = hash.getHash("HEX");
var password = document.createElement("input");
password.name="password";
password.type="hidden";
password.id = "password";
password.value = hash;
alert(password.value);
form.appendChild(password);
form.submit();
pwd.value = "";
}

Answer

In your onsubmit handler, you're submitting the form:

 form.submit();

If you do that, the handler has to return false, which means you need

<form ... onsubmit="submitForm(); return false;">

Otherwise you will submit the form manually, then the browser will submit it a second time, since onsubmit didn't return false;

Comments