简体   繁体   中英

Cakephp 2, back/cancel button to go to the previous page

If I have a form, and don't click submit before hand, using standard javascript history.

<button onclick="history.go(-1);">Back </button>

will bring me back to the previous page. However, if I click submit, and error come out (validation). the back button will bring me back to the same page (the page before the submit error happen).

How can I create a back button where it always bring to the previous page, regardless error of validation.

Thank you

Going by the actual history isn't a great way of doing what you want. What you see happening here, is of course expected behavior, since submitting the form over and over will just add the page with the form to the history over and over. And by design, for security reasons, there isn't a way to check arbitrarily far back to the last page the user was on on your site that wasn't the form.

Therefore, I think a solution in Cake would be better. If the user can only arrive on the page from one place, you could make the button just link back to that page, rather than depending on the history. Alternatively, I would store in session the page that the user came from originally, and be careful to not rewrite it as long as the user stays on the form, and then link to that on the back button.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM