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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dave Smithz

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

    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?








  • Dave

    #2
    Re: Refresh problem - last command gets re-executed - how to prevent?

    Dave Smithz wrote:[color=blue]
    > 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.
    >[/color]

    ....snip...
    [color=blue]
    > Any suggestions?
    >[/color]

    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("Locatio n: " . $_SERVER["SCRIPT_NAM E"]);
    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...

    Comment

    • devans14@gmail.com

      #3
      Re: Refresh problem - last command gets re-executed - how to prevent?

      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

      Comment

      • Sadara

        #4
        Re: Refresh problem - last command gets re-executed - how to prevent?

        Dave Smithz wrote:[color=blue]
        > 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?[/color]

        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

        Comment

        Working...