dongwen2896 2016-11-10 15:13
浏览 127
已采纳

PHP Javascript更改浏览器后退按钮行为Laravel

I know there are various threads that ask for nearly the same but none of them seems to really satisfy my needs....

On my site, I implemeneted a search form. A simple form with an input field, called searchQuery and a submit button. The form is sent with POST method. I'm using Laravel btw.. The search result are then loaded into a view from the controller. Those are shown in a table. Now comes the interesting part: The found elements are clickable and you get on a page with more details about that element. Then the problem comes: When I now click the browsers back button or my back button on the mouse, I get a window asking to send the form information again and I have to confirm that.

This is not what I want, I want to just get back to the search results I had before....

Now I read a lot, but am still not knowing, how to do that.

I read before onbeforeunload where you can display a message, but thats not useful for me (I want to get back to the search results)...

So there are some alternatives that would be possible (if technically possible):

  1. just prevent the dialog asking to submit the information again and just submit it again without asking for
  2. Somehow go back in the history to the page before (cached maybe or something similiar, search results won't change in that time)
  3. if I wanted to, I could include the search them in the url like http://servername/search/searchquery, then pass this searchquery into the deatil view and have a button to go to that url that then does the search again or something like that. Then you could (maybe) override the browsers back button to "press" that button instead of going back in history...

Is one of these options possible and if yes how? Or is there any other way you can advise?

  • 写回答

1条回答 默认 最新

  • dongwu5318 2016-11-10 15:29
    关注

    As per your comment in the question above, you can do it like this:

    First of all your form should be like:

    <form action="/search" method="GET">
        <input type="text" name="searchQuery" value="Type keywords here...">
        <input type="Submit" value="Search">
    </form>
    

    Then after submitting your form, the route / url would be like this:

    http://localhost:8000/search?searchQuery=any_search_keyword
    Route::get('search', 'SearchController@handleSearch'); // Laravel route
    

    And you can handle your search in your controller like this:

    function handleSearch() {
        $searchQuery = request()->get('searchQuery');
        // ... Handle your search here ...
    }
    

    In this way you can use GET method to handle searches, this also prevents the unnecessary submission dialog over and over again on going back and forth...

    As per my understanding this would be the solution you're looking for

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题