By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,712 Members | 1,440 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,712 IT Pros & Developers. It's quick & easy.

Refresh problem - last command gets re-executed - how to prevent?

P: n/a
Hi there,

Working on the PHP DB I took over I have just come across a problem. When
the user presses refresh in some circumstances the command they just
performed will be re-performed. In some cases this is very undesirable.

Further example:

There is a PHP page in my web application that has a text box and the
ability to add a note to the account the user is currently viewing. When the
user presses submit the page reposts to itself with a hidden command
parameter so the form knows what to do before redisplaying the account
details (with the new note).
However if the user presses refresh, the command gets re-executed again and
we now have another identical note added to the account.

Not desirable behaviour particularly when it comes to editing / deleting
items etc.

Bearing in mind this is a large application that will have many pages to
change and a Monday deadline for new changes, what is an easy way I can
prevent this? How do other people cope with this? Maybe from the start the
whole thing should have been approached differently but I have to work with
what I have.

Any suggestions?


Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Dave Smithz wrote:
Hi there,

Working on the PHP DB I took over I have just come across a problem. When
the user presses refresh in some circumstances the command they just
performed will be re-performed. In some cases this is very undesirable.

....snip...
Any suggestions?


One solution which would be a quick and reasonable fix, would be to add
something like the following two lines, right after the DB updates...

header("Location: " . $_SERVER["SCRIPT_NAME"]);
exit();

This redirects the browser back to the current page, clearing all of the
form vars. Might not be exactly what you want though - for one, it does
not take account of vars that might be tacked onto the url, so you'll
want to deal with that as appropriate...
Jul 17 '05 #2

P: n/a
I ran into the same problem. Just letting the the script fall thru and
display a new page invites problems. I ended up having the script
redirect back to itself after making sure everything was cleaned up.
That way if the user manually refreshes a page, that's all they get, no
duplicate operations.

The other thing I've used on some projects is to have a centralized PHP
page to process database operations. It redirects back to the parent
page when it's done. Each calling page would have a hidden input value
that the db page keys on so it knows what to do.

Both of them work like a charm.
Daniel

Jul 17 '05 #3

P: n/a
Dave Smithz wrote:
Hi there,

Working on the PHP DB I took over I have just come across a problem. When
the user presses refresh in some circumstances the command they just
performed will be re-performed. In some cases this is very undesirable.

Further example:

There is a PHP page in my web application that has a text box and the
ability to add a note to the account the user is currently viewing. When the
user presses submit the page reposts to itself with a hidden command
parameter so the form knows what to do before redisplaying the account
details (with the new note).
However if the user presses refresh, the command gets re-executed again and
we now have another identical note added to the account.

Not desirable behaviour particularly when it comes to editing / deleting
items etc.

Bearing in mind this is a large application that will have many pages to
change and a Monday deadline for new changes, what is an easy way I can
prevent this? How do other people cope with this? Maybe from the start the
whole thing should have been approached differently but I have to work with
what I have.

Any suggestions?


i have a hidden field on all forms identifying the form:

<input name="formID" type="hidden" value="<?= time() ;?>">

when an insert is performed, the 'formID' is inserted into a database
table called 'formIDs'.

an insert can only be performed if the formID is not already in the , i
check that the formID is not already in the table 'formIDs'.

if you refresh, the formID does not change, so there will be no insert
performed.

it is *extrememly* unlikely that two forms will have the same 'formID' -
but not absolutely impossible.

sadara

Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.