Page 1 of 1

double submitting

Posted: Sun Jun 10, 2012 7:54 am
by roice
Hello,

From time to time I found duplicated records (rows) in my database. Its looks like the user double click on the submit button.
recently its happen more often so I add code that check when the last submit happened (I save the linux time) and check if there is 10 second delay:

Code: Select all

	$query = mysql_query("SELECT create_date FROM `tkts_topics` WHERE (client = '$client') AND (email = '$email') AND (title = '$title') AND ($date - create_date <= 10) LIMIT 0,1");
	if (mysql_num_rows($query))
	{

}
I don't know why but its not working.
is there any better way to prevent double submiting?

Roi.

Re: double submitting

Posted: Sun Jun 10, 2012 12:41 pm
by Tiancris
If you are suspecting a double sending data, you could try disabling the button when the user clicks.

Code: Select all

<form>
<input type="button" onclick="buttonClick(this)">
</form>
<script type="text/javascript">
function buttonClick(btn)
{
  btn.disabled = true;
  btn.form.submit();
}
</script>

Re: double submitting

Posted: Sun Jun 10, 2012 4:22 pm
by requinix
Server-side there are a couple common options:
- Check for recent duplicate data. Certainly easier to do but it might not always be possible.
- Generate a random ID, stick that in the session and hide it in the form, and when the form is submitted check that the ID hasn't been "used" yet. That could be the fact that it's still stored in the session: if it's set then the ID is valid, you do the work, and unset it, and if it's not set then you know the form was submitted twice.

Re: double submitting

Posted: Sun Jun 10, 2012 5:07 pm
by headbull
What can happen is also when you submit forms, and the user refreshes the "loading" page he submits the data again. A solution to this can be to redirect the user with header(); to a page that confirms the submission (submitconfirm.php?submitid=XXX <- if you need the data reviewed/confirmed without the risk of submitting it again).