Jeffrey Pratte wrote:
> Gang,
> 
> I am occasionally getting duplicate records in my database. I think it is 
> coming from people hitting “Submit” twice when the server is slow. How do 
> you guys prevent this? 
> 
> I tried some JavaScript code to disable the button, which did gray-out the 
> button, but then did not pass the Button info along to Cold Fusion.
> 
> Thanks for you help, Jeff

There's a few ways.

The simplest I can think of is to create a data structure (a database
table would be sufficent) to keep track of your forms where you want
to prevent multiple submissions. The schema of a table to do this would
be something like the following:

CREATE TABLE forms (uuid CHAR(35) NOT NULL PRIMARY KEY);

Before you generate a form, do the following:

<cfset uuid = CreateUUID()>
<cfquery name="foo" datasource="bar">
INSERT INTO forms
(
     uuid
)
VALUES
(
     <cfqueryparam cfsqltype="CF_SQL_CHAR" value="#uuid#">
)
</cfquery>

And then embed the uuid in a hidden field in the form you want to
protect.

Just before you process the form, check to see if the uuid is in the
table. If it is, it's the first time the form has been processed,
otherwise it's a resubmit.

<cfquery name="checkUUID" datasource="bar">
SELECT  COUNT(*) AS isPresent
FROM    forms
WHERE   uuid = <cfqueryparam cfsqltype="CF_SQL_CHAR"
                     value="#FORM.uuid#">
</cfquery>
<cfif checkUUID.isPresent>
     <cfquery name="foo" datasource="bar">
     DELETE FROM forms
     WHERE  uuid = <cfqueryparam cfsqltype="CF_SQL_CHAR"
                         value="#FORM.uuid#">
     </cfquery>
     <!--- Process as normal --->
<cfelse>
     <!--- It's a resubmit --->
</cfif>

Now, where is a chance of a race condition in the code above where, if
the user was quick enough on the resubmit (and they'd have to be
*really* quick), the code to check if the form was present might be ran
in one of the requests before the first one managed to delete it. To
prevent this, you'd have to serialise access to the forms tables
somehow, be it through a transaction or an exclusive table lock.

But the principle is sound and should work for you.

K.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Logware (www.logware.us): a new and convenient web-based time tracking 
application. Start tracking and documenting hours spent on a project or with a 
client with Logware today. Try it for free with a 15 day trial account.
http://www.houseoffusion.com/banners/view.cfm?bannerid=67

Message: http://www.houseoffusion.com/lists.cfm/link=i:4:201399
Archives: http://www.houseoffusion.com/cf_lists/threads.cfm/4
Subscription: http://www.houseoffusion.com/lists.cfm/link=s:4
Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4
Donations & Support: http://www.houseoffusion.com/tiny.cfm/54

Reply via email to