#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    8
    Rep Power
    0

    Error message after submitting a form?


    Hey guys, I need a little help here.

    I don't know how to prevent a user from clicking the back arrow in the browser after a form has been submitted. What I don't want is I don't want the user to submit the same form over and over again by moving back and forth, and in any case, if he/she wanted to do that then it would have to be done from scratch (at least by refreshing the page so that the form is reset).

    Any help? suggestions? thanks a lot.
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1045
    Hi,

    Simply do a redirect after the submit. See the Post/Redirect/Get pattern.

    That's what all online shops do in their checkout process.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    8
    Rep Power
    0
    Hello Jaques,

    Thanks for the suggestion. I understand the concept but I still don't know how to do it in practice in PHP. I probably didn't give enough information about my problem and where I'm at, here's a bit of background:

    What I have is a page with an article I wrote. At the bottom of it there's a comment section.

    Assuming the user enters the correct details e.g. name, email, comment, the form gets submitted and the user gets redirected to a "thank-you.php" page. On this new page there's a link the user is supposed to click that will take them back to the page where I wrote the article.

    When the user clicks the link that takes him back there's no problem, the page gets refreshed the way I want. Problem is when instead of clicking that link the user clicks the back arrow in the browser, basically what happens is that the user is taken exactly to the same line where the comment section is on my page.

    Based on your suggestion, I've been reading about PRG pattern and I can't really find a practical example, the examples I've found look complicated, I don't think what I need is such a big deal?

    In an article I read they recommend this:

    header(‘HTTP/1.1 303 See Other’);
    header(‘Location: URL’)

    However, that doesn't work for me. What am I doing wrong?

    Thanks!
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1045
    What does "not work" mean? Do you get an error message? A header() call must appear before any output.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    8
    Rep Power
    0
    What I mean is that it seems like the first header is not read at all. My second header IS read though because the user still gets redirected to "thanks.php", but then when I try and click the back arrow the user is sent back to the previous page, which is just what I'm trying to prevent.

    These are the two lines I have:

    header("HTTP/1.1 303 See Other");
    header("Location: thanks.php");

    "A header() call must appear before any output."

    Sure, I know that. Like I said, my second header works fine, the problem is in the first one. :/
  10. #6
  11. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1045
    The first header has no visible effect, so whatever problems you experience have nothing to do with the header being or not being sent. But you can check the exact response with the developer tools of your browser.

    OK, I guess I misunderstood you. So you want to completely remove the page from the browser history? That's a bad idea.

    First of all, it's not user-friendly. If I want to view my form input again (for whatever reason) and you don't let me, chances are I'll leave your site and never come back. People expect their browser to behave in a certain way, and if a website breaks this pattern, they'll be annoyed.

    Secondly, it's simply uneffective. What prevents me from duplicating the form tab or simply opening the page again? If you want to prevent multi-submits, you need a time limit or check for identical texts.

    And last but not least, it's not possible with standard HTML. You need to use JavaScript's location.replace() -- but this can simply be deactivated by anyone.

    So I wouldn't do it. Use an intelligent method like a text check.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    8
    Rep Power
    0
    Thanks Jacques, good points! so I'll break down your comments.

    Code:
    The first header has no visible effect, so whatever problems you experience have nothing to do with the header being or not being sent. But you can check the exact response with the developer tools of your browser.
    Nice, I'll check the developer tools and see if I can find what's going on.

    Code:
    OK, I guess I misunderstood you. So you want to completely remove the page from the browser history? That's a bad idea.
    
    First of all, it's not user-friendly. If I want to view my form input again (for whatever reason) and you don't let me, chances are I'll leave your site and never come back. People expect their browser to behave in a certain way, and if a website breaks this pattern, they'll be annoyed.
    I agree!

    I don't necessarily want to remove the page totally from the history but I want to force the user to refresh the page. The problem is that when the user clicks the back arrow, he is taken back exactly to where he left off. He's basically taken to the same line where the comment section is at. Nothing so wrong with that, the problem is that the text he entered is still in the form and if he clicks the Submit button again the form is resubmitted. Now, you said something good in the next paragraph.

    Code:
    Secondly, it's simply uneffective. What prevents me from duplicating the form tab or simply opening the page again? If you want to prevent multi-submits, you need a time limit or check for identical texts.
    I'll try those suggestions, a time limit or check for identical texts, that's probably the way to go about it.

    Code:
    And last but not least, it's not possible with standard HTML. You need to use JavaScript's location.replace() -- but this can simply be deactivated by anyone.
    Sure, I don't want to use JS for any of that stuff as it's not so reliable, it can be simply turned off. That's why I'm trying to do it with PHP but I'm having a hard time with the actual code.

    However, the text check sounds good to me, I'll try that and get back to you when I'm done, thanks a lot
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    8
    Rep Power
    0
    Done! the duplicate check did the trick, thanks Jacques

IMN logo majestic logo threadwatch logo seochat tools logo