views:

116

answers:

1

Here is something that I find disturbing. I created a small form, and I'm using AJAX to validate for me. I have a javascript function authenticate() that works sometimes.

<form method="post" action="" id="login_form" onsubmit="authenticate()";>

// various inputs

<input type="button" onclick="authenticate()" value="Log In">

</form>

authenticate() works just fine when I click the button. However, if I press enter the form is submitted, and it fails. It also fails if I call onSubmit(). In my debugging, I alert the outgoing texts-- they are identical. However, the Prototype Ajax function calls it the onSuccess, but there is just no response from the server. (The server outputs either "Success" or "Failure").

Why the different behaviors for onClick() vs onSubmit()? The exact same function is called, but the results are different.

Any help would be appreciated.

--Dave

+2  A: 

The button's onClick does not submit the form. It is merely a button that calls authenticate() on the client side.

Your onSubmit event will be called when the form submits, but you may not be seeing the result of the client side authenticate() because the form will post immediately after. It's not clear what functionality is intended.

But, if authenticate() does what I guess, then you need to change the attribute to onSubmit='return authenticate()' and let the function return true/false. When false, the form submission will be aborted.

spoulson
"...the form will post immediately after..."I think I understand what is happening now. I actually don't want a submission, but I like the form behavior, and Prototype provides a nice serialize function for forms.Thanks!
the Hampster
We can validate form first by doing button's "onclick" function, and can submit the form once validation passes. So we don't need use submit() functions.
pramodc84