Everything2
Near Matches
Ignore Exact
Full Text
Everything2

HTML form double submit bug

created by elluzion

(thing) by elluzion (18.7 hr) (print)   ?   (I like it!) Thu Oct 31 2002 at 21:49:42

Find It.

Situation: You have a perfectly good web page which contains a form. When the form is submitted, it submits twice when you expect it to only submit once. This can lead to lots of problems.

Assumption: The form is submitted by clicking an image input object (<input type="image">), which triggers a JavaScript function that in turn submits the form.

In short, you have something similar to this:


<html>

<head>

<title>My broke-ass webpage</title>

<script language="JavaScript">

	function someFunction()
        {
          //... do some fun javascript

          document.myHtmlForm.submit();

	}

</script>

</head>

<body>

<form name ="myHtmlForm" action="http://some.web.site/cgi-bin/someCgi" method="post" >

<input type="image" src="myImageFile.jpg" onclick="someFunction()">

</form>

</body>

</html>

Understand it.

Essentially the problem is the image input type. An under-documented feature of the image input is that it is actually a submit button. It behaves exactly the same as a submit input type. So, when the image is clicked, the form is submitted, period. However, in this case, the image also triggers a JavaScript function that also submits the form. So this is how your form is being submitted twice. Once for the image click, again for the JavaScript function.

Fix it.

There are plenty of ways to fix this problem. First, you could just not use image inputs. Secondly, (and this is my favorite option) you could not use JavaScript. However, knowing that neither of these options are likely to be obeyed, I'll explain a workaround for this problem.

Basically, you have to add "return false" to your onClick attribute. Yeah, I know. It's odd. But trust me. It works. Just go from using

<input type="image" src="myImageFile.jpg" onclick="someFunction()"> 

and instead, use

<input type="image" src="myImageFile.jpg" onclick="someFunction(); return false">

It disables the default behavior for the image input type. I don't quite understand why it works, but it does.

Hopefully this will help someone.


lj says: you could also just use an <IMG> instead of <INPUT type = "image">, as onclick is a valid attribute for <IMG> (as well as, i think, any block tag.)

Reply: This is true, although not all browsers support onClick attributes for plain images.


printable version
chaos

How not to be a 15 year old JavaScript hack 15 year old JavaScript hack Making your web site more cache friendly abusive JavaScript
nothing2.com HTML heading tags Helping people cheat JavaScript
understand HTML Notelet nodelet HTML tricks Notelet Nodelet Squawkbox Client
B Br HTML attributes HTML tags
Cite code A INS
abbr Christopher Acronym em
Y'know, if you log in, you can write something here, or contact authors directly on the site. Create a New User if you don't already have an account.
  Epicenter
Login
Password

password reminder
register

Everything2 Help

Cool Staff Picks
Nodes to live by:
The Years of Rice and Salt
Stephen Foster
blood pressure
Protocols of the Meetings of the Learned Elders of Zion
23
Heinz Kohut
Smalltown
Oranges & Lemons
Halcyon
When your job is keeping order, your life becomes chaos
Ice cream
Manfred von Richthofen
Teletubbies
New Writeups
fallensparks
George's Marvellous Medicine(thing)
Ctrl Y
cognitive dissonance(fiction)
SharQ
Gone Baby Gone(review)
halfWit
If I could, I'd title this "Freedom"(thing)
Roninspoon
Airline Hero(thing)
Ktistec
Why Women Are Always Cold(person)
doctor wilson
Drug policy reform(thing)
tejasa
Easy Raspberry Cheesecake(recipe)
Joysim
Drug policy reform(idea)
aneurin
Tyburn(place)
niruena
Boiling to death(idea)
artman2003
summer(thing)
doctor wilson
The Silver City and the Silent Sea(log)
Dreamvirus
The Silver City and the Silent Sea(poetry)
Aerobe
A nihilist's soulmate(poetry)
This page courtesy of The Everything Development Company