Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Submit a form once

Status
Not open for further replies.

ahksar

Programmer
Jan 11, 2001
43
US
I was wondering if coldfusion could be used to ensure that a user submits the information in a form just once, and prevent multiple submissions of the same data.I tried using javascript, but it didnt solve the problem.
Thanks
ahksar
 
You could set a session variable such as "session.formSubmitted" to "yes" for the first submision. You could then check to see if "session.formSubmitted" exists and not process the submission if it does.

I believe this should do what you want.

<cfif isdefined(&quot;session.formSubmitted&quot;)>
..... Put a message saying they've already subitted, redirect to another page, etc...
<cfelse>
.... Code to handle the submission .......
<cfset session.formSubmitted = &quot;yes&quot;>
</cfif>

This method assumes the visitor will only need to fill out the form once, if multiple submissions of different data is necessary, you'll have to add some coding to clear the session variable.

Hope this helps,
GJ
 
GunJack, I think ahksar means that he wants to prevent impatient users from submitting one and the same form a couple of times (is that right ahksar?). You know, if it takes some time before the form is actually submitted, some users keep on clicking the submit button hoping it will speed things up :).
ahksar, the following Javascriot can be used prevent this:

<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
var submitted=0;

function checkSubmit() {

if (submitted == 0)
{
submitted++;
return true;
}
else
{
return false;
}
}
</SCRIPT>

<form onSubmit=&quot;return checkSubmit()&quot; method=&quot;post&quot;>
<input type=submit value=&quot;Submit!&quot;>
</form>

Hope this helps...

<webguru>iqof188</webguru>
 
and a simple trick is also to disable the submit button so the user can't click twice
in ie4+ simply add the bold line :

document.formName.submitButtonName.disabled=true;
 
True iza, but as you said that only works in IE4+. :)

<webguru>iqof188</webguru>
 
Yea, I've used JS to prevent duplicate submissions but I've found it's not always reliable. Since he said he didn't have good luck with JS and asked for a CF solution, I posted one which I believe will work. Anytime I use JS to prevent the user from doing something undesireable on the client side, I like to have CF double check and prevent it as well on the server side in case of a JS problem with earlier browsers or if they have it turned off altogher.

One of the simplest JS check routines I've seen was this,

<INPUT TYPE=&quot;Button&quot; NAME=&quot;Submit&quot; VALUE=&quot;Submit Form&quot; onClick=&quot;if(this.value == 'Submit Form') this.form.submit(); this.value = 'Please Wait.';&quot;>

GJ
 
i agree with GJ, the CF solution is safer, but it's faster for the user to also have a simple jscript that reacts - that's why i also use both (a very light jscript, as the one you're mentionning or the disable trick if i'm sure of the user browsers; and then the session stuff in cf)
 
I had already tried the code iqof said, and while it worked , people could still submit the form more than once. DEfining a session variable worked though. Thanks for all your help.
ahksar
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top