Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Form Resubmission

  • This topic is empty.
Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #170742
    chrisburton
    Participant

    Is there a way to not show the form resubmission alert on refresh? I’m also trying to stick to one php file.

    #170746
    __
    Participant

    You mean the browser warning? It happens because the browser recognizes that you’re POSTing the same info you already POSTed.

    There’s a trick, it’s simple but the idea is little hard to follow:

    Say the user submits value=hello to your post.php page. That means the POST and the page will be associated in the browser’s history. (And so when refreshing or backing up, it warns you that you’re submitting the form again: this is what causes the problem.)

    The solution is to make sure that, when the user submits the form, the page that it is associated with is never returned: if it is never returned, it won’t be in the browser history, so the user will never be able to refresh/backup to it.

    You can do this with a redirect (i.e., Location header).

    I’ve found the simplest approach is:

    • when you get a POST (form submission), save the POST data to the session.
    • Do not output (print) anything.
    • send a Location header, redirecting back to the same page.
    • when the session has POST data in it, you know there’s a form submission to be processed.
    • from this point, you can continue processing the form and printing the output as normal.

    outline:

    <?php
    session_start();
    
    if( $_POST ){
        // put form submission in session
        $_SESSION["POST"] = $_POST;
        // redirect and stop script immediately
        header( "Location: http://example.com/this/page.php" );
        exit;
    }
    if( isset( $_SESSION["POST"] ) ){
        // oooh! a form submission!
        // pretend it was in POST all along
        $_POST = $_SESSION["POST"];
        unset( $_SESSION["POST"] );
    }
    
    // continue processing/output as normal
    
    #170748
    chrisburton
    Participant

    Oooh! Nice. Let me give that a shot.

    #170749
    chrisburton
    Participant

    @traq Hmm. Having trouble on the last bit.

    from this point, you can continue processing the form and printing the output as normal.

    How can I process the form into a database if it’s unset? Use $_POST again?

    #170751
    chrisburton
    Participant

    Nevermind. I can just do this:

    if($_POST){
        $_SESSION['POST'] = $_POST;
        header('Location: http://something.com');
        exit;
    }
    
    if(isset($_SESSION['POST'])) {
        $_POST = $_SESSION['POST'];
        unset($_SESSION['POST']);
    }
    
    if(!empty($_POST)) {
    //do database stuff here
    
    #170753
    __
    Participant

    How can I process the form into a database if it’s unset? Use $_POST again?

    Yup, that’s what I meant (intended). Should’ve been more clear.

    This can get more complicated; if you’re uploading files, for example. But it’s the same idea.

    #170757
    chrisburton
    Participant

    Thanks for the help on this one @traq. After researching on SO, I only found methods by either using two separate files or AJAX. I didn’t want to use either of those options so this helped a lot.

    #170760
    __
    Participant

    No problem. I went looking for the article that originally “clued me in” to this issue+solution, but it was years ago (and, as I recall, was a long, difficult search in the first place). Oh well. I’ve memorized it.

    #170802
    chrisburton
    Participant

    and, as I recall, was a long, difficult search in the first place

    I’m a bit surprised this isn’t a well documented solution. Oh and I’m going to use this for my Kirby Comment System as well.

    #170807
    __
    Participant

    Oh and I’m going to use this for my Kirby Comment System as well.

    Whup, well, now I want royalties : p

    I’m a bit surprised this isn’t a well documented solution.

    Hmm. Maybe I should document it…

Viewing 10 posts - 1 through 10 (of 10 total)
  • The forum ‘Back End’ is closed to new topics and replies.