Link to home
Start Free TrialLog in
Avatar of bpirrong
bpirrong

asked on

OnClick vs OnChange causes different behavior inserting records into the database with Windows IE

I'm stumped.  I've got a text box with an 'Add' button next to it.  The text box has an onChange = doSubmit2('Save').  The Add button has an onClick=doSubmit2('Save').   The doSubmit2 inserts a record into a database table.  If I use the onChange event, it works just fine.  If I use the onClick event, two records are inserted into the database.  This only happens under Windows IE (I'm using 6.0.2900 on Windows XP SP2).  It doesn't happen on any of the Mac browsers (IE, Safari, Mozilla) and it doesn't happen on Mozilla on Windows XP SP2).  I've tested under IE 6.0.2800 and 5.50.xxx on Windows and the same behavior happens.

Going into the cfquery tag, I don't have any records in the database.  Immediately after the cfquery tag, I've got two identical records in the database.  I've used the following syntax for my insert "insert into table_name (field1, field2, field3) values (element1,element2,element3)", as well as "insert into table_name set field1 = element1, field2 = element2, field3 = element3".  There are no triggers on the database.

I've also removed the cftry/cfcatch tags at this point.  I can catch the double insertion with cftry/cfcatch, but in the case of the User trying to insert two records, I'd like to actually show an error message.

Anyone have any ideas?

Thanks,
Beth
Avatar of INSDivision6
INSDivision6

If you have "Add" button defined as <input type=submit ...> than you have two events "OnClick" and "submit".  So you are submitting the form twice.  Define your button, as <input type="button" ...>  or return 0 from JS call to disable default behavior (form submit) on button click.
Avatar of bpirrong

ASKER

The add button is an image, not an input button.  Sorry, should have said that initially.
Still, if it is defined <input type="image" ...> this is the same issue.  Make it just <img>.  To test this, add a global var that will disable second submit:

var submitted=0;

doSubmit(but)
   {
   if (submitted) return 0 ;   // do not submit more than once
   submitted=1;
   . . . . . . . . .
   return 0;
   }
BUT, you still need to add:  onClick="javascript:doSubmit2('Save'); return 0;"  for <input type="image">
The add button is defined as follows:
<img src="../images/add_up.gif" alt="Add" name="Image18" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image18','','../images/add_dwn.gif',1)" onClick="doSubmit2('Save')">

The text box is defined as follows:
<input name="aiminv_tag_num" type="text" size="20" maxlength="20" value="<cfif IsDefined( "get_inventory" )><cfoutput>#get_inventory.tag_num#</cfoutput></cfif>" onChange="doSubmit2('Save')"/>

I've tested to see if it was being submitted twice and it's not.  Plus, if it was an issue of submitting twice, that would happen on both the onChange and the onClick and on the other browsers.  Like I said, this one's got me stumped!

BTW, doSubmit2 simply sets a value for a different form element and then does document.form1.submit();

Thanks for the suggestions though - got any other ideas? :-)
If you have a duplicate record, and this is a browser-dependent behavior, the form must be submitted twice.  If to try replace <img> with a regular submit, what happens?  There are known differences between IE and Netscape in processing clicks for <input type="image">, but if you say you are using regular <img> it should be the same.
Also try setting your database so that duplicate records can not be created.

Also you could try a javascript solution like these to prevent duplicate submissions:

http://www.netmechanic.com/news/vol5/html_no16.htm
http://www.web-source.net/web_development/form_submission.htm
At first I also thought it must be submitting twice.  So, I removed all cflocation tags - the action on my form tag returns me to the current page.  I created a query to count the number of records that are in this table prior to running the insert query and display the results.  I then run the insert query and display a message.  Following that is another query to count the number of records that are in the table.

When I submit the page, it comes back with:

current records 0.
insert successful.
new records 2.

Now, if it were submitting twice, the entire section of code would be run twice, so I would see:
current records 0.
insert successful.
new records 1.
current records 1.
insert successful.
new records 2.

So, this tells me I'm only submitting once - Correct?  Or am I missing something?  I'll try replacing the img with a regular submit and see what happens.

I can set the database to not accept duplicate records but then I either a)incorrectly tell the user they've tried to insert the same record twice or b)never tell the user when they have actually tried to insert the record twice.  Right now, as a work-around, I've gone with option b - never tell the user.  
ASKER CERTIFIED SOLUTION
Avatar of INSDivision6
INSDivision6

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
What database are you using?

If you are using MS SQL server you can run the profiler to see exactly what is happening from the database's point of view....
Well, I haven't been able to duplicate this issue in a new coldfusion form, and I've verified that it's not submitting twice, so I think I've hit some very weird combination of activities that's causing the insert on just this one section to be done twice.  I have no idea.  I put an insert into a different table JUST ABOVE this problematic insert, and it's only inserting once into that table.  Odd.

Anyway, I've decided to cut my losses and work around the issue.  Thanks for your suggestions - I appreciate the time you spent on this!