SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Addict sojomy's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Finally! Here's how to stop the refresh reload button problems!

    You know how you make a form that processes data, and the darn end-users keep pressing the refresh or reload button that adds multiple rows to the database or keeps trying to delete data that isn't there after they delete it once? Well here's how I keep data from being reprocessed when a user pressed the refresh button. The code is first, the explaination is below it.


    PHP Code:
    <? session_start(); ?>
    <HTML><BODY>
    <?
    if (!isset($_POST['Action'])) $_POST['Action'] = '';
    if (!isset(
    $_SESSION['FormProcessed'])) $_SESSION['FormProcessed'] = 0;
    if (
    $_POST['Action'] == 'Register') {
      if (
    $_SESSION['FormProcessed'] == 1) {
        exit(
    'You have been registered.</BODY></HTML>');
      }
      
    dbConnect();
      
    $SQLStatement 'INSERT INTO Users SET '
                    
    'FirstName=\'' $_POST['FirstName'] . '\', '
                    
    'UserName=\'' $_POST['UserName'] . '\'';
      if (!
    $SQL mysql_query($SQLStatement)) {
        exit(
    'An error occurred.  Please try again later.</BODY></HTML>';
      }
      
    $_SESSION['FormProcessed'] = 1;
      exit(
    'You have been registered.</BODY></HTML>');
    }
    $_SESSION['FormProcessed'] = 0;
    echo 
    '<FORM NAME="ContactForm" ACTION="' $_SERVER['PHP_SELF'] . '" METHOD="POST">'
       
    '<INPUT TYPE=HIDDEN NAME="Action" VALUE="Register">'
       
    'First Name : <INPUT TYPE=TEXT NAME="FirstName" VALUE="' $_POST['FirstName'] . '">'
       
    'User Name : <INPUT TYPE=TEXT NAME="UserName" VALUE="' $_POST['UserName'] . '">'
       
    '<INPUT TYPE=SUBMIT VALUE="Register">'
       
    '</FORM>';
    ?>
    </BODY></HTML>

    Basically, this is a registration form, that submits to itself, and then inserts the submitted data into the database. I stripped the page of almost all error checking, so you will need to add that yourself. This is only an example to show how to keep data from being reprocessed (redeleting, reupdating or reinserting to or from the database) every time the refresh button is pressed by the user. I have also not checked this code, so it's not guaranteed to be 100% syntax correct or working. But the theory is good


    The first thing done is starting the session. Then checks to see if the $_POST['Action'] and $_SESSION['FormProcessed'] variables are already set. If not, they are set to default values. If this is the first time the page is loaded, then it wasn't submitted, so the $_POST['Action'] variable is empty, so the first half is skipped and the form is displayed. Before the form is displayed, the $_SESSION['FormProcessed'] variable is set to 0 (zero). This is necessary if you use more than one form on your website and only use one SESSION variable for every form, or if you want forms to be able to be submitted more than once without the user having to log out and then back in (close the session).


    The user enters his First Name and a User Name and presses submit. The form POSTs to itself, and the page is reloaded with POSTed data. When the form is loaded with POSTed data, if the $_POST['Action'] variable is set to "Register", then the first half of the page is executed. First it checks to see if the $_SESSION['FormProcessed'] variable is set to 0 or 1. If it is set to 0, then we know that we still have not added this user to the database. So we process the data - we add the user to the database. If it is successful, then we set the $_SESSION['FormProcessed'] variable to 1 and we display a confirmation message. If the $_SESSION['FormProcessed'] variable was already set to 1 when this page was loaded, then we know that the form was already processed (the user was added to the database already, and they pressed refresh), so we simply display the Confirmation screen again, without doing any database processing.


    This is how I keep all of my forms from being refreshed and having extra data added or re-updated in my databases. If anyone has any questions or comments, please let me know. Or if you have any better ways as well.

  2. #2
    Ceci n'est pas Zoef Zoef's Avatar
    Join Date
    Nov 2002
    Location
    Malta
    Posts
    1,111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for sharing, Sojomy. Nice solution!

    I've been thinking about this as well. I was thinking about something along these lines: (haven't tried it yet)
    PHP Code:
    <?php 
    if (isset($_GET['registered']) && ($_GET['registered'] == 1))
        {
    /* Show confirmation message and exit */}
    if (
    $_POST['submit'] == 'Register')
        {
        
    // Process submitted 'POST' data and then redirect:
        
    header('Location: ' $_SERVER['PHP_SELF'] . '?registered=1'); exit();
        }

    /* if none of the above: show form */
     
    ?>
    Any thoughts?

    Rik
    English tea - Italian coffee - Maltese wine - Belgian beer - French Cognac

  3. #3
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You'd be implying that it submits them to file.php with $_POST['submit'] as "Register", then the processing is done after redirection? Could work..

  4. #4
    Ceci n'est pas Zoef Zoef's Avatar
    Join Date
    Nov 2002
    Location
    Malta
    Posts
    1,111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes on the first part of your sentence, no on the second.

    Processing after redirecting would not work because the 'POST' variables will be lost by redirecting.

    That's actually the principle of the thing. After processing (store in db, whatever... ) the posted data, redirect so that the posted data will be lost. That way, they can hit 'refresh' as much as they want, there's no more data there!

    Is that making any sense?

    Rik
    English tea - Italian coffee - Maltese wine - Belgian beer - French Cognac

  5. #5
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OH, it submits, then redirects to thank you page.

    Looks like it'd work, but I think the session idea looks good.

    Set session on post screen, if already set, leave it. On submit, chcek if it's set. If it is, process then destroy session. If it's not set, go back to form.

  6. #6
    ********* Genius Mike's Avatar
    Join Date
    Apr 2001
    Location
    Canada
    Posts
    5,458
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Set the form action page to send a no content header and use some css/js to hide the form and display a "thank you" not when the submit button is clicked, after x seconds or so.
    Mike
    It's not who I am underneath, but what I do that defines me.

  7. #7
    SitePoint Addict sojomy's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Zoef
    Thanks for sharing, Sojomy. Nice solution!

    I've been thinking about this as well. I was thinking about something along these lines: (haven't tried it yet)
    PHP Code:
    <?php 
    if (isset($_GET['registered']) && ($_GET['registered'] == 1))
    {
    /* Show confirmation message and exit */}
    if (
    $_POST['submit'] == 'Register')
    {
    // Process submitted 'POST' data and then redirect:
    header('Location: ' $_SERVER['PHP_SELF'] . '?registered=1'); exit();
    }

    /* if none of the above: show form */
    ?>
    Any thoughts?

    Rik
    Rik, I like your idea too, but what if the end user presses the back button, and then refresh (I know that they shouldn't, but they always seem to do it anyway [img]images/smilies/smile.gif[/img] )? It would reprocess the data and redirect them again, right? If you have set a session variable, then even if they press back and refresh it, it won't reprocess the data...I think [img]images/smilies/smile.gif[/img]

  8. #8
    SitePoint Evangelist anjanesh's Avatar
    Join Date
    Jun 2004
    Location
    Mumbai
    Posts
    447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also when using header we are taking into consideration that the browsers accepts redirects. If it doesn't or if its disabled then how could it work ?
    Anjanesh


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •