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 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误