...

View Full Version : To Prevent double submission



charon
06-18-2007, 09:29 AM
Hi,

How do I prevent user to reload/refresh or click on others option in the page after page submission? As this will cause it to reprocess the data which have already submitted.

pls advise.....

Daemonspyre
06-18-2007, 04:31 PM
You can do this by 1 of 2 ways:

The more preferred (and easiest) is to use Javascript. Check out the web for some "prevent double submission" scripts.

The other method is to track something useful from your client computers (IP, hostname, browser, etc) using the Request.ServerVariables methods and track them in a database. Once you have that, then do a simple search and compare against the submission. If any records match, kill the submission.

If you want to disable the refresh button or the back button, then you need to consider changing your code.

HTH!

miranda
06-18-2007, 08:20 PM
To prevent duplicate data when users either double clicking and/or click the back button and make a change in the data that they submitted you can also change your Database code to look for the existance of the data and if it exists then you Do an update instead of an insert. In SQL Server I use stored procedures to handle all input and will write my SPROCS accordingly. If you use inline SQL you can just add something like this



sSQL = "SELECT FieldA, FieldB, FieldC FROM MyTable WHERE FieldA = '" & sFieldA & "' AND FieldB = '" & sFieldB & "' AND FieldC = " & iFieldC
oRs.Open sSQL, myConn
'If Using ADO
If oRs.EOF Then oRs.AddNew()
oRs("FieldA") = sFieldA
oRs("FieldB") = sFieldB
oRs("FieldC") = iFieldC
oRs.Update()
oRs.Close
'If Using SQL Inserts And Updates
If oRs.EOF Then
oRs.Close
sSQL = "INSERT INTO MyTable(FieldA, FieldB, FieldC) VALUES("' & sFieldA & "','" & sFieldB & "'," & iFieldC & ");"
oRs.Open sSQL, myConn, 1,2
Else
oRs.Close
sSQL = "UPDATE MyTable SET FieldA = '" & sFieldA & "', FieldB = '" & sFieldB & "', FieldC = " & iFieldC WHERE MyIDField = " iID
oRs.Open sSQL, myConn, 1,2
End If

the above assumes that MyIDField is either an Access AutoNumber Field or a SQL Server Identity

charon
06-22-2007, 09:35 AM
Hi,

I prefer to use javascript, so I have tried with below method:

function checkReload()
{
if( document.formtest.checksub.value == "" )
{
document.formtest.checksub.value = "1";
}
else
{

alert("Your submission is in the process......Please wait, Resubmission is not allowed")
}
}


</script>

<body onLoad="checkReload();">

<form name="formtest">
testing
<input type="hidden" name="checksub" value="" />
</form>

as I know that In most web browsers, hidden form variables are maintained across refreshes. That is, even though a page is refreshed, if a hidden form variable was changed by JavaScript before the refresh, the changed value will be used after the refresh. Thus, on a page load JavaScript can check to see if a hidden variable that defaults to being unset is set. If it is unset, it assumes this is not a refresh and it sets it. If it is set, it assumes this is a refresh.

But I just wonder why the value of checksub won't change after checkReload, so the above method won't work for me.....kindly help.

Well, I do found exactly the same method used, here to go http://www.tedpavlic.com/post_detect_refresh_with_javascript.php, I have tried their sample, cannot work as well.

Pls advise....

charon
06-23-2007, 06:49 AM
Hi,
Can this thread be removed to "Javacript" category?

pls advise....

glenngv
06-24-2007, 07:38 AM
as I know that In most web browsers, hidden form variables are maintained across refreshes. That is, even though a page is refreshed, if a hidden form variable was changed by JavaScript before the refresh, the changed value will be used after the refresh.
That's not correct. And Javascript is not a reliable solution. The user can easily disable Javascript and then submit the page twice. The recommended solution is to keep track the time the user submits the form in a session variable and set the allowable time (say 10 sec) that same user (using his IP address) can submit again. If the same IP submits the page again in less than 10 secs, then stop processing the page. This is I think what CodingForums do when you do a search in its Search page.

charon
06-24-2007, 04:03 PM
HI,


as I know that In most web browsers, hidden form variables are maintained across refreshes. That is, even though a page is refreshed, if a hidden form variable was changed by JavaScript before the refresh, the changed value will be used after the refresh.

You mean that above statement is incorrect?


The user can easily disable Javascript and then submit the page twice
Actually, I have been struggling about this when want to use the javascript in my application. Yup, I do agree this, so nowaday, what are the best solutions about this? Does it mean that javascript is not longer use as client side scripting?

Pls advise......

Spudhead
06-26-2007, 04:53 PM
You mean that above statement is incorrect?

Makes sense to me. Form values are maintained through refreshes, and I can't see that it makes a difference whether those values were changed by the user manually or by javascript automagically.


Does it mean that javascript is not longer use as client side scripting?

No, not at all. Otherwise what would all those AJAX-powered sites do? But you're talking about using JavaScript to validate a user action that could potentially break your back end system. This is not advisable. Sure, use JS for simple validation and stuff - but as an aid to the user, not as your actual "this is the bit that stops bad stuff happening to my database" code. If it's that critical to your application, check it server-side.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum