RE: Re[4]: [PHP-WIN] Preventing multiple submits

From: Date: Fri, 25 Aug 2000 14:32:21 +0000
Subject: RE: Re[4]: [PHP-WIN] Preventing multiple submits
References: 1  Groups: php.windows 
Request: Send a blank email to php-windows+get-2147@lists.php.net to get a copy of this message
Okay I'm being REALLY thick. I'm new to PHP, HTML and programming in general and that's the excuse I'm using! I understand the following code and it's aim but I'm having problems actually implementing it: if (! $post_counter) // handle form submission else print "Your form has been sent"; if ($HTTP_POST_VARS[Submit]) $post_counter = 1; I've got TellAFriend.php (which contains the HTML form) and POST's to TellAFriendSubmit.php. When the email details are entered correctly by the user an email is generated by TellAFriendSubmit.php. If errors are present then TellAFriend.php is redisplayed with the following command: header("Location:TellAFriend.php?$Error"); $Error contains the required variables to re-popular the form fields, etc. Once all fields have passed validation then the email is generated and sent. There is a small delay whilst this is occurring and the form is still onscreen. The user could hit the "Submit" button again, which runs the TellAFriendSubmit.php script again. Every time they hit submit an additional email is generated. I only want to run the script the first time. To do this I need some way of incrementing/setting a variable when the Submit button is pressed and then pass it on to TellAFriendSubmit.php. The problem is whatever way I try and implement this the variable is only ever incremented by 1 regardless of how many times the user hits the submit button. Therefore the code I've written in TellAFriendSubmit.php that checks for multiple presses of the submit button doesn't function correctly and multiple emails are still sent if the user is click happy on the submit button. I must be missing something very simple! PLEASE HELP. I want to keep this solution as easy as possible and don't really want to use database access to solve it, etc. Sorry for these questions but I've got a headache. Maybe I need a rest! Thanks James PS: If it helps I can include my PHP scripts (but no laughing at my poor code!). -----Original Message----- From: Zak Greant [mailto:zak@php.net] Sent: 24 August 2000 23:22 To: Pablo Vera Cc: James Duncan; php-windows@lists.php.net Subject: Re[4]: [PHP-WIN] Preventing multiple submits At 05:11 PM 8/24/00 -0500, Pablo Vera wrote: >[snip] >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 $post_counter variable is not stored client side. All that you store client side is a session id in either a cookie or as get/post data. The $post_counter variable is tracked server side by PHP's session manager. (You could also use another mechanism, if desired.) - zak >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. > >Pablo.

« previous php.windows (#2147) next »