We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Why does my form submit twice?

Medium Priority
1,522 Views
Last Modified: 2013-11-19
Hi Folks,

I have this form that submits twice for some crazy reason.  I just can't work out *why*.  The submit is done via an image button that fires a javascript function with onClick() event.

When I watch this action with fiddler tool, I see that the browser posts twice.  I can make it post just once if I remove the line

//document.login.submit();

but why does it submit at all?  I am assuming that 'image' form field behaves like 'button' - perhaps it realy behaves like 'submit' type?

Any clues will be gratefully accepted!

Thanks and regards,  Mike.
<script language="javascript">
var clicked = false;
function do_login()
{
	if (! clicked )
	{
		clicked = true;
		document.login.submit();
	}
	else return false;
}
</script>
 
<form name="login" action="http://login.host/login" method="post">
username: <input style="width: 240px" name="username" type="text" value="" /><br>
password: <input style="width: 240px" name="password" type="password" /><br>
<a onClick="return do_login()" target="_top" onMouseDown="" onMouseUp="">
<input type="image" name="loginbutt" src="templates/loginbutt.gif" alt="Log into system" value="LOGON">
</a>
</form>

Open in new window

Comment
Watch Question

CERTIFIED EXPERT
Expert of the Year 2008
Top Expert 2008
Commented:
input type="image" IS a submit button. So you are submitting via the image and via the onclick on the "a" tag. Get rid of the "a" tag.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Top Expert 2008

Author

Commented:
ahah,

>> input type="image" IS a submit button

OK, so i can get rid of the anchor tag, and put onClick="do_login()" for the image tag instead?

Cheers.
CERTIFIED EXPERT
Expert of the Year 2008
Top Expert 2008

Commented:
>>onClick="do_login()" for the image tag instead
Yes, but you don't need the onclick at all. The input type="image" will submit when clicked.
Top Expert 2008

Author

Commented:
OK,

>> The input type="image" will submit when clicked.

yes, but I also want to prevent a double-click causing multiple submit events, so I am thinking that this would do it:

onClick="return do_login()"

how ironic that I wanted to prevent a double click to cause duplicate POST events, and ended up causing duplicate POST every time! ;-D

Cheers.
CERTIFIED EXPERT
Expert of the Year 2008
Top Expert 2008

Commented:
>>onClick="return do_login()"
OK, them make sure you return false in your function always.
function do_login()
{
        if (! clicked )
        {
                clicked = true;
                document.login.submit();
        }
return false;
}
 
when this is executed:
 document.login.submit();
 
the form will be submitted regardless of what's the return value. But if you return false always, you are making sure that the submission is NOT triggered by the input element, which is what you want since you are doing the submission via javascript.

Open in new window

Top Expert 2008

Author

Commented:
OK, thanks!

This is what I have settled on.  I know that the 'return true;' is probably technically not required, but I figure that it is worth putting it in there for 'readability'.

Your assistance is very much appreciated!

Cheers.




function do_login()
{
        if (! clicked )
        {
                clicked = true;
                return true;
        }
return false;
}
 
<form name="login" action="http://login.host/login" method="post">
username: <input style="width: 240px" name="username" type="text" value="" /><br>
password: <input style="width: 240px" name="password" type="password" /><br>
<input onClick="return do_login()" type="image" name="loginbutt" src="templates/loginbutt.gif" alt="Log into system" value="LOGON">
</form>

Open in new window

Top Expert 2008

Author

Commented:
Thanks!
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.