PowWeb Forums - The Perfect Community for the Perfect Host  

Register now to interact with over 11,000 members! Registered users have Posting Privileges, free access to Private Messaging, Email Notifications and more.

Go Back   PowWeb Community Forums > Web Site Design > HTML/CSS/Javascript
User Name
Password
Register FAQ Members List Search Today's Posts Mark Forums Read

Closed Thread
 
Thread Tools Search this Thread
Old 4-15-07, 10:41 PM   #1
Sparhawk
 
Join Date: May 2002
Location: Idaho
Posts: 261
Reputation: 32
FireFox submits form twice?

I have a couple of forms that are misbehaving in FireFox, but work fine in IE.
In all the affected forms:
* The person enters data into forms, using any combination of text fields, textareas, radio buttons, and SELECT fields.
* The person clicks a button that says "submit" (which is really just a button, not a submit button).
* A javascript function validates all of the fields entered, and stops the submission if there is an error.
* Once the validation is complete, and the form is ready for submission, the script calls " document.myForm.submit()" to submit the form.

Sample (obviously not working code):
Quote:
<html>
<head>
<script>
function submitMe()
{
// do something, like form field validation before submitting
if (ready to submit) {
document.myForm.submit();
} else {
return false; // do nothing, do not submit the form
}
}
</script>
</head>
<body>
<form id="myForm" name="myForm" action="form.cgi">
Some form fields here<br/>
<input type="button" value="Check First" onclick="submitMe();">
</form>
</body>
</html>

(notice the form doesn't really have a "submit"-type button)

THE PROBLEM:
In IE, the script performs as expected.

In FireFox (tested in v2.0.0.3):
* if the form validates correctly (tested by the " if (ready to submit) " above), it gets submitted twice: once with all of the data, and once with all fields blank (or default).
* if the form fails validatation, it gets submitted once with all fields blank (or default).


Am I coding something wrong here? Why is FireFox submitting the form when I don't specifically call the "document.myForm.submit()" function?

Last edited by Sparhawk : 4-15-07 at 11:02 PM. Reason: clarifying the problem & symptoms
Sparhawk is offline  
Old 4-16-07, 09:06 AM   #2
tpoynton
Custom User Title
 
tpoynton's Avatar
 
Join Date: Sep 2004
Location: Mass
Posts: 1,765
Reputation: 208
I'm a total hack at this kind of thing...but when I use javascript validation for a form, I set the onsubmit property of the opening form tag to run the validation routine, then use the action property to call the form processing script. it appears as though the data are submitted twice (or at least in FF) - once through the action, and once through the myform.submit?

again, total hack, could be way off...
tpoynton is offline  
Old 4-16-07, 12:27 PM   #3
Sparhawk
 
Join Date: May 2002
Location: Idaho
Posts: 261
Reputation: 32
Thanks for the suggestion, but I tried that. Even changing my form tag to:
Quote:
<form id="myForm" name="myForm" action="form.cgi" onsubmit="submitMe();">

... and changing my submit button to:
Quote:
<input type="submit" value="Check First">

The form still gets submitted twice. Same situation: one with the correct data, and one with blank or default data.
Sparhawk is offline  
Old 4-17-07, 08:12 PM   #4
rainbore
Rick
 
Join Date: May 2002
Location: Minneapolis, MN
Posts: 1,515
Reputation: 174
Insert 'return true;' on a new line after the document.form.submit(); I suspect that the function is apparently always returning a result of 'false' which is why Firefox assumes that it has to allow the HTML 'submit' button's submit function to be executed.
rainbore is online now  
Old 4-25-07, 02:08 PM   #6
prafullapalwe05
Registered
 
Join Date: Apr 2007
Location: UK
Posts: 1
Reputation: 0
any solutions..

Hey guys,
I faced a similar probelm while developing a massive network fault diagnosis application where in i run some masive tests which may take around 2-3 minutes to complete.
And some users of this applications with firefrox browser complained that the tests are run twice when they submit the form.
In the action of the form i am actually calling another perl file which initiates the tests by calling binary on AIX platform.

I was clueless about this problem and dont know whats happenning here.
After some debugging i found that the action is verifies in firefrox and thats the place the action is getting called up twice.

Any pointers in this regard would be really helpful.
prafullapalwe05 is offline  
Old 5-1-07, 05:38 PM   #7
satis
 
satis's Avatar
 
Join Date: Oct 2002
Location: Dallas
Posts: 2,699
Reputation: 213
what happens if you use this?

<input type="button" value="Check First" onclick="submitMe(); return;">
__________________
Satis Clankiller
Clankiller.com Forums
Clankiller.com
PlasmaSky.com
satis is offline  
Old 5-26-07, 11:11 PM   #8
Sparhawk
 
Join Date: May 2002
Location: Idaho
Posts: 261
Reputation: 32
I figured it out (with the help of W3Schools.com)!

For others dealing with the same problem, here is the adjusted pseudo-code:
Quote:
<html>
<head>
<script>
function checkAndSubmitMe()
{
// do something, like form field validation before submitting
if (ready to submit) {
return true; // submits the form
} else {
return false; // do nothing, do not submit the form
}
}
</script>
</head>
<body>
<form id="myForm" name="myForm" action="form.cgi" method="post" onsubmit="return checkAndSubmitMe()">
Some form fields here<br/>
<input type="submit" value="Check First">
</form>
</body>
</html>



Doing it this way works with both IE and FireFox.
Here's how things work:
* The person clicks the "Check First" button.
* The "checkAndSubmitMe()" function is run, returning 'false' if the form should not be submitted, or 'true' if the form should be submitted.
* If 'true' is returned, then the "action" of the form is submited.

Last edited by Sparhawk : 5-27-07 at 04:10 AM.
Sparhawk is offline  
Old 5-27-07, 03:19 AM   #9
Sparhawk
 
Join Date: May 2002
Location: Idaho
Posts: 261
Reputation: 32
False alarm.
It seemed to work for a while, but now it is back to doing the same thing again.
Sparhawk is offline  
Old 5-27-07, 03:59 AM   #10
Sparhawk
 
Join Date: May 2002
Location: Idaho
Posts: 261
Reputation: 32
My adjusted code above still exhibits this problem:

Quote:
Originally Posted by Sparhawk View Post
In FireFox (tested in v2.0.0.3):
* if the form validates correctly (tested by the " if (ready to submit) " above), it gets submitted twice: once with all of the data, and once with all fields blank (or default).

But it does resolve this problem:

Quote:
Originally Posted by Sparhawk View Post
* if the form fails validatation, it gets submitted once with all fields blank (or default).


And, some more information:
My CGI "action" script sends an e-mail to me with the contents of the form. When the person uses IE, I get the one correct e-mail submission. When the user browses with FireFox, I get one correct submission and one blank submission.

Using the Perl variable $ENV{'REQUEST_METHOD'}, I checked to see how the form was getting submitted. To do this, I simply included the value of $ENV{'REQUEST_METHOD'} in the text of the e-mail submission.
On the first (correct) submission, the request method is POST, as I wrote in the form:
Quote:
<form id="myForm" name="myForm" action="form.cgi" method="post" onsubmit="return checkAndSubmitMe()">
On the second (blank) submission, the request method is GET. This is not written anywhere in my code.
Sparhawk is offline  
Old 5-27-07, 06:42 PM   #11
Sparhawk
 
Join Date: May 2002
Location: Idaho
Posts: 261
Reputation: 32
Oh good God, that was stupid. I figured out the problem. Although I don't konw why IE & FireFox behave differently, this is what the situation was:

Every page on my site has a left border menu written in JavaScript. Included in that menu is a randomly-selected image.

Once the person clicks the submit button and goes to the confirmation screen (the same page that sends the e-mails), the randomly-selected image is displayed again on the left menu.

The JavaScript code for that image tag was:
Quote:
document.write(' <img id="randompicimg" name="randompicimg" src="" height="50px" width="50px">');

The picture filename and the link (to the full-size picture) were intentionally left blank, because further down the page there is code that fills it in.

It appears that when given an IMG tag with no associated file, the browsers treat the "image" differently:

In Internet Explorer:
The right-click "Properties" of the missing image in IE is attached as "ie_missingimageproperties2.JPG". If the SRC of the IMG tag is "", IE substitutes the current folder URL, and shows a "broken" image:
Name:  ie_missingimageproperties2.JPG
Views: 1949
Size:  10.0 KB
In effect, IE treats the IMG tag like (italics is an example):
Quote:
<img id="randompicimg" name="randompicimg" src="www.myfakesite.com/currentfolder" height="50px" width="50px">
Since it can't display an HTML file as an image, it displays the 'broken image' picture.


In FireFox:
The right-click "Properties" of the missing image in FireFox is attached as "ff_missingimageproperties2.JPG". If the SRC of the IMG tag is "", FireFox substitutes the current filename URL, and shows a broken image:
Name:  ff_missingimageproperties2.jpg
Views: 1944
Size:  10.0 KB

In effect, FireFox treats the IMG tag like (italics is an example):
Quote:
<img id="randompicimg" name="randompicimg" src="www.myfakesite.com/currentfolder/form.cgi" height="50px" width="50px">
Since it can't display an HTML file as an image, it displays the 'broken image' picture.
Because FireFox replaces a missing IMG SRC with the URL of the current page, and the current page is the "form.cgi" file that grabs the form data, sends the e-mails and displays the confirmation screen, the CGI script gets run twice: the first one as expected, the 2nd one in the blank image spot with no POST data, and shows as a GET instead.

So, to resolve the problem, I substituted the code with:
Quote:
document.write(' <img id="randompicimg" name="randompicimg" src="blank.gif">');
where "blank.gif" is a 1x1-pixel white box, until the script further down the page places the correct random picture in the spot.

This way, FireFox doesn't run the script a 2nd time from the IMG tag.


The most annoying thing about this problem is that I didn't think the IMG tag would have anything to do with how the form got submitted.
Sparhawk is offline  
Old 5-27-07, 07:55 PM   #12
phlembol
 
Join Date: Apr 2007
Location: USA
Posts: 402
Reputation: 26
Quote:
Originally Posted by Sparhawk View Post
.. where "blank.gif" is a 1x1-pixel white box..

Transparent would be a better choice in case the background color changes. I've seen that used a lot.


__________________

-- Vinyl records for your collection at Noble-Efforts.com ---- Find a record turntable at BuyRecordPlayers.com --
-- Set your sweet tooth on fire at FlamingHotCandy.com ---- Looking for a GPS device? Check buyGPSdevice.com --
-- Check out my WordPress experiment: Defend Your Right To Offend at phreedom.us --
phlembol is offline  
Old 5-27-07, 07:58 PM   #13
Sparhawk
 
Join Date: May 2002
Location: Idaho
Posts: 261
Reputation: 32
Quote:
Originally Posted by phlembol View Post
Transparent would be a better choice in case the background color changes. I've seen that used a lot.



True, but the script loads fast enough that the graphic isn't even seen.
And since the only image editor I have is MS Paint, it doesn't do transparent GIFs, does it?

Or do you have a 1x1 transparent GIF you could attach in this thread for me to use?
Sparhawk is offline  
Old 5-27-07, 08:46 PM   #14
phlembol
 
Join Date: Apr 2007
Location: USA
Posts: 402
Reputation: 26
Sure. Haven't used Paint since Win3.1, couldn't tell you its current limitations.

Can you find it? Try this: <<URL removed at request of poster>>

Attached Images
 
__________________

-- Vinyl records for your collection at Noble-Efforts.com ---- Find a record turntable at BuyRecordPlayers.com --
-- Set your sweet tooth on fire at FlamingHotCandy.com ---- Looking for a GPS device? Check buyGPSdevice.com --
-- Check out my WordPress experiment: Defend Your Right To Offend at phreedom.us --

Last edited by Doc C : 5-29-07 at 03:51 PM. Reason: URL removed at request of poster.
phlembol is offline  
Old 5-29-07, 03:57 PM   #15
phlembol
 
Join Date: Apr 2007
Location: USA
Posts: 402
Reputation: 26
I really did attach a 1x1 transparent gif file, but how do you right-click on a 1x1 invisible picture? I see it has been downloaded 14 times, must be a way. Anyone?


Never mind, I'm a little slow, but I found it.
__________________

-- Vinyl records for your collection at Noble-Efforts.com ---- Find a record turntable at BuyRecordPlayers.com --
-- Set your sweet tooth on fire at FlamingHotCandy.com ---- Looking for a GPS device? Check buyGPSdevice.com --
-- Check out my WordPress experiment: Defend Your Right To Offend at phreedom.us --
phlembol is offline  
Old 5-29-07, 04:19 PM   #16
Doc C
Moderator
 
Doc C's Avatar
 
Join Date: Mar 2006
Location: Southern CA
Posts: 5,044
Reputation: 286
Hold down your left mouse button and move the cursor from left to right starting on the left side of the Attached Images box. You should get a highlighted area. That is the gif file.

Right click to Copy and then paste it somewhere on your hard drive.
__________________
"You don't really understand human nature
unless you know why a child on a merry-go-round will wave at his parents every time around
-- and why his parents will always wave back."

-William D. Tammeus

Last edited by Doc C : 5-29-07 at 07:59 PM. Reason: somewhere not someone.
Doc C is offline  
Old 5-29-07, 07:40 PM   #17
phlembol
 
Join Date: Apr 2007
Location: USA
Posts: 402
Reputation: 26
Or, from the "Threads in Forum" view, click on the paperclip icon for the thread to download any of the attachments in that thread.
__________________

-- Vinyl records for your collection at Noble-Efforts.com ---- Find a record turntable at BuyRecordPlayers.com --
-- Set your sweet tooth on fire at FlamingHotCandy.com ---- Looking for a GPS device? Check buyGPSdevice.com --
-- Check out my WordPress experiment: Defend Your Right To Offend at phreedom.us --
phlembol is offline  
Old 5-29-07, 08:00 PM   #18
Doc C
Moderator
 
Doc C's Avatar
 
Join Date: Mar 2006
Location: Southern CA
Posts: 5,044
Reputation: 286
Or click your heels three times and say "there's no place like home". Whooops. That's not right.
__________________
"You don't really understand human nature
unless you know why a child on a merry-go-round will wave at his parents every time around
-- and why his parents will always wave back."

-William D. Tammeus
Doc C is offline  
Closed Thread


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT -4. The time now is 09:30 PM.


Contents ©PowWeb, Inc. ~ vBulletin, Copyright © 2000-2007 Jelsoft Enterprises Limited.