开发者

Why does my form data disappear when the user is taken back to the page?

We have a form on our web site that's generated using some very simple PHP. No javascript or anything is involved, and the form is the same every time.

If the user fills out the form incompletely, they're taken to a page that says "Hey, please go back and fill in all the fields". When they hit the back button, they're taken back to the form.

In Chro开发者_JAVA技巧me, the form data they entered is still there. But in Firefox (at least in firefox 3.6), all the form data has gone.

We don't do anything special to preserve the form values. Why is Chrome saving them and Firefox isn't? What's the "standard" thing we should be doing to preserve them?

Thanks.


Most browsers load the cached page when clicking on the back button, but I'd guess that FF3.6 doesn't. The browsers that doesn't load the cached version of the page will do a new GET to your server which will render an empty form when they click the back button.

But I think you should consider re-thinking your solution actually. Because the normal behavior is to post to the same page and then show the same page with pre-filled form values if the user entered the form incorrectly. And if the user entered the form correctly a good thing to do is to do an actual redirect to the same page (or another page for that matter). This is called the PRG pattern.


As you have noticed, whether the form values will be there after hitting the Back button is browser dependent. As far as I am aware, the standard solution is to not have a separate page for the error message, but rather respond with a page that contains the same form, pre-filled with the values the user entered (and preferrably with an indication of which fields need to be completed).

As an example: the form is at /form.php, and looks like this:

<form action="/form.php" method="POST">
     <input type="text" name="foo" />
     <input type="submit" value="Submit" />
</form>

In /form.php (pseudo-code; I almost never code PHP):

if (this is a GET request) {
    echo the form;
}
else (if this is a POST request) {
    if (field values are correct) {
        processValues(...);
        redirectToConfirmationPage();
    }
    else {
        echo "<form action='/form.php' method='POST'>";
        echo "    <input type='text' name='foo' value='$theValueTheUserEntered'/>";
        echo "    <span class='error'>Field is incorrect or empty</span>";
        echo "    <input type='submit' value='Submit' />";
        echo "</form>";
    }
}


Maybe it's just a personal setting of your current Firefox installation (ie. nevers save data-forms).

Try it if works on another PC with firefox.


It's a behavior unique to the individual browsers themselves; there is nothing you can do server-side to force all browsers to "remember" the data that was there.

To work around this, try redisplaying the form with the valid values that the user had entered already dumped in as the "default" values of the form fields. It's what most of the big name web apps do.


Some browsers remember forms, some don't. That's why you need to have the form repopulate values in PHP if it's already submitted.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜