Re: Avoiding user refresh of pages with forms

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




----- Original Message ----- From: "Rory McKinley" <rorymckinleylists@xxxxxxxxx>
To: <php-general@xxxxxxxxxxxxx>
Sent: Wednesday, May 03, 2006 8:32 PM
Subject: Re:  Avoiding user refresh of pages with forms


Brad Bonkoski wrote:
Is there a way to key off of the data inserted? Like some unique value or set of values that you can do a quick lookup before you insert/update/delete again... Or you could venture into an AJAX style of submission keyed off of a button click then a refresh to a 'report' page, in which case no POST variables are actually passed to the report page, so refreshing it will just present the same data...

-Brad

Jeff wrote:

Is there a way to prevent a re-posting of the form data when a user
pushes the "refresh" button on the browser?
<snip>

To follow on from what Brad said - would it be possible to generate a "key" as a hidden value when the form is created. The first time the user submits the server checks if that unique code has ever been used within the current session - if not, adds value. If so, ignores.

As a "unique" key you could use say timestamp plus random number.

HTH

Rory


I used that method initially, some months ago, but finally dropped it. It looked nice at first, but then I started getting into problems and required too many special cases to make it work. In the end, it wasn't a clean nor elegant solution.

Currently, my simple php files (that is, those not generated by frameworks) have three sections, the form, the update SQL section and the confirmation section.

To put it into pseudo-code, the thing goes like this:

if ($_REQUEST['submit'] == 'Ok') {
   // validate data
// if there are any errors I concatenate the messages in a $error_messages variable. / Do not output anything (error message or other status) until after the redirect below
   if (// no errors) {
       // update, insert, delete or whatever you have to do
// if everything goes Ok, I do a redirect to this same page with a new argument 'confirm'
       // and whatever is needed to show a confirmation message
// Both the redirect() and build_url() functions are in my include, I hope their purpose is obvious. redirect(build_url('thispage.php',array('confirm' => true,'otherdata' => 'values')));
   }
}
if ($_REQUEST['confirm']) {
   // here you show the confirmation message.
// if there had been any errors, it wouldn't come here, no need to check that
   // and refresh or back would fall here, not on the code above
}

// this is an option, if you want to show the form along the confirmation message, don't put the else

else {
// here goes the form. The submit button name and value should match the values of the first if // if there were any error message, they should be shown here, either before, after, or mixed withing the form fields // if the form is shown along the confirmation, it might load the previous values but then, be careful, duplicate records might be inserted.
}

Satyam

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux