Re: Preventing multiple submits
| From: | Manuel Lemos | Date: | Fri, 25 Aug 2000 05:45:51 +0000 |
| Subject: | Re: Preventing multiple submits | ||
| References: | 1 | Groups: | php.windows |
| Request: | Send a blank email to php-windows+get-2131@lists.php.net to get a copy of this message | ||
Hello Pablo,
On 24-Aug-00 19:11:33, you wrote:
>>>Are you saying by default that as soon as PHP receives a form it doesn't
>>>accept any more forms from the same client?
ZG>> No. I am saying that your script should work this way.
ZG>> ie.
ZG>> if (! $post_counter)
ZG>> // handle form submission
ZG>> else
ZG>> print "Your form has been sent";
ZG>> if ($HTTP_POST_VARS[$submit])
ZG>> $post_counter = 1;
>Zak:
>I think that the "if (! $post_counter)" will always result in a true
>condition, this is because the only ways to preserve a variable from
>instance to instance of a script is by sending them in the URL or by
>setting a cookie, but since the form is being submitted twice from
>exactly the same page, you will have two scripts running at the same
>time, with exactly the same variables and values, and the output of
>the first one will not be received by the browser, because the second
>one canceled that connection. You will end up processing two forms,
>but the user will receive confirmation for only one.
The best solution for really preventing more that one submission of the
same form on the server side is to use create a unique key and pass it in
the form as hidden field. Store the key in a database (session data area
is ideal for this). When the first form submission is received, check if
the key exists in the database, delete it, accept the form. On further
submissions, check that the key does no longer exist and warn the user that
the form was already submitted.
>I know that JavaScript (or any other Client-Side tool) has its
>limitations as noted by you before, but with it you will stop the
>second submission before it is actually sent. The first script will
>run normally and return its output to the browser, without knowing
>that the user tried to send the form for a second time. The browser
>will no cancel that first connection, because it is not posting a
>second form.
Right, you may want to check this PHP form generation and validation class.
It has an option to let you specify a text that is used to pop up a
confirmation box with Javascript that asks the user if he really means to
submit the form again. The user may confirm or cancel the form submission.
This really works great since most of the times you just want to prevent
nervous users to submit forms more than once creating inadverted duplicate
database entries.
http://phpclasses.UpperDesign.com/browse.html/package/1
Regards,
Manuel Lemos
Web Programming Components using PHP Classes.
Look at: http://phpclasses.UpperDesign.com/?user=mlemos@acm.org
--
E-mail: mlemos@acm.org
URL: http://www.mlemos.e-na.net/
PGP key: http://www.mlemos.e-na.net/ManuelLemos.pgp
--