Jump to content

Preventing browser refresh from reposting form values -- cache_limiter?


macworks

Recommended Posts

I'm wondering how to prevent a page refresh from re-posting the form values so that PHP won't re-process the same form submission twice.

For example, I'm building my own custom message board and when a poster submits their new post, they get the result page which shows that their message was successfully posted. But if they refresh the browser window, they'll end up posting the message again.

Is this where session.cache_limiter comes in?

I realize I could send another header once the post has been logged into the DB in order to forward on to yet another page (preventing this refresh scenario) but I'd prefer a slightly easier method of preventing the refresh from happening altogether.
Link to comment
Share on other sites

I ahve had this problem before on other websites. Maybe you could use javascript, unless php does it, to disable the refresh button for that one specific page only, it will prevent people from refreshing the page, then you could leave a little message there saying that the button has been disabled to prevent double posting.
you can use one of the following scripts I tracked down for you to stop the refresh and f5 buttons from functioning in that window, if one doesn't work try the other, the second has been tested in some of the newer browsers, so pick and choose. Or use them all for double effect I guess.

This is javascript code by teh way./
[code]
if (document.all){         
    document.onkeydown = function (){
            var key_f5 = 116; // 116 = F5        
                 
        if (key_f11==event.keyCode){
                            alert("F5 pressed");
                return false;
        }
    }
}

[/code]
[code]
function showDown(evt) {
        evt = (evt) ? evt : ((event) ? event : null);
        if (evt) {
            if (event.keyCode == 8 && (event.srcElement.type != "text"
&& event.srcElement.type != "textarea" && event.srcElement.type !
= "password")) {
                // When backspace is pressed but not in form element
                cancelKey(evt);
            }
            else if (event.keyCode == 116) {
                // When F5 is pressed
                cancelKey(evt);
            }
            else if (event.ctrlKey && (event.keyCode == 78 ||
event.keyCode == 82)) {
                // When ctrl is pressed with R or N
                cancelKey(evt);
            }
        }
    }

    function cancelKey(evt) {
        if (evt.preventDefault) {
            evt.preventDefault();
            return false;
        }
        else {
            evt.keyCode = 0;
            evt.returnValue = false;
        }
    }

    // Additional code for NS
    if (navigator.appName=="Netscape") {
        document.addEventListener("keypress",showDown,true);
    }
    document.onkeydown  = showDown;
[/code]
If you know javascript then you might want to make sure you put necessary tags while you go, like the <script language="javascript" type="text/javascript">
</script>
or you might be able to just stick those in the header section.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.