Curing the “Back Button Blues”

走远了吗. 提交于 2019-12-02 20:59:53

That is a good discussion but more to the point you should be looking into Post Redirect Get (PRG) also known as "Get after Post."

http://www.theserverside.com/patterns/thread.tss?thread_id=20936

If you do not understand my article then you should take a close look at figure 1 which depicts a typical scenario where a user passes through a series of screens – logon, menu, list, search, add and update. When I describe a movement of FORWARDS I mean that the current screen is suspended while a new screen is activated. This happens when the user presses a link in the current screen. When I describe a movement as BACKWARDS I mean that the user terminates the current screen (by pressing the QUIT or SUBMIT button) and returns to the previous screen, which resumes processing from where it left off. This may include incorporating any changes made in the screen which has just been terminated.

This is where maintaining a page stack which is independent of the browser history is crucial – the page stack is maintained by the application and is used to verify all requests. These may be valid as far as the browser is concerned, but may be identified by the application as invalid and dealt with accordingly.

The page stack is maintained by two functions:

  • scriptNext() is used to process a FORWARDS movement, which adds a new entry at the end of the stack and activates the new entry.
  • scriptPrevious() is used to process a BACKWARDS movement, which removes the last entry from the stack and re-activates the previous entry.

Now take the situation in the example where the user has navigated to page 4 of the LIST screen, gone into the ADD screen, then returned to page 5 of the LIST screen. The last action in the ADD screen was to press the SUBMIT button which used the POST method to send details to the server which were added to the database, after which it terminated automatically and returned to the LIST screen.

If you therefore press the BACK button while in page 5 of the LIST screen the browser history will generate a request for the last action on the ADD screen, which was a POST. This is a valid request as far as the browser is concerned, but is not as far as the application is concerned. How can the application decide that the request is invalid? By checking with its page stack. When the ADD screen was terminated its entry was deleted from the page stack, therefore any request for a screen which is not in the page stack can always be treated as invalid. In this case the invalid request can be redirected to the last entry in the stack.

The answers to your questions should therefore be obvious:

  • Q: Where do you call each function?
  • A: You call the scriptNext() function when the user chooses to navigate forwards to a new screen, and call the scriptPrevious() function when the user terminates the current screen.
  • Q: Which function should be called first and which next, and which third?
  • A: Each function is called in response to an action chosen by the user, so only one function is used at a time.
  • Q: Will all functions be called in all files in an application?
  • A: All functions should be available in all files in an application, but only called when chosen by the user.

It you wish to see these ideas in action then you can download my sample application.

The part I'm particularly interested in is controlling the back button in order to prevent form duplicate entries into a database when the back button is pressed.

Your premise is wrong. There is no such thing as "Back Button Blues", if you design your application as a web application. If you design your application without any server side state, you will never run into this problem in the first case. This minimalistic approach to web applications works remarkably well, and is usually known as REST.

@ troelskn

If you design your application without any server side state ....

It is not possible to design an effective application which does not have state, otherwise all you have is a collection of individual pages which do not communicate with each other. As maintaining state on the client is fraught with issues there is no effective alternative but to maintain state on the server.

@Marston.

I solved the problem with post/redirect/get but I believe the tutorial has some merit and perhaps Tony Marston can elaborate on it. And how it could be used to solve not necessarily my particular problem but perhaps something similar. Or how is it better than post/redirect/get if the functions can in fact be used in solving my particular problem. I think this will be a good addition to the community here.

if ($_POST) {
    process_input($_POST);
    header("Location: $_SERVER[HTTP_REFERER]");
    exit;
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!