duanfan8360 2014-10-19 02:05
浏览 36

PHP - 分页:$ _POST或$ _GET

I'm developing a search system(multiple filters) that uses pagination with php. At first i was using the method POST with the main form. But, using POST i was unable to keep the pagination. Reason: when the user search by name, for example, as he clicks in the next pages the query get lost and return to the first page. To fix this i use GET method instead. But using GET, the url gets the parameters. And the users don't want that.

Example:

http://mysearch.com/search.php?name=joe&id=1

I just want to be

http://mysearch.com/search.php

I tried this workaround:

if(typeof window.history.pushState == 'function') {
        window.history.pushState({}, "Hide", "http://mysearch.com/search.php");
}

But when i hit the "Previous Page/Back" in the browser, the URL with the parameters come back either.

Is there a solution for this? Use GET or POST with pagination and the parameters does not shows in the URL?

  • 写回答

1条回答 默认 最新

  • duanmei1694 2014-10-19 02:30
    关注

    You can paginate with $_POST just as easily as with $_GET and without SESSION variables/cookies, you just need to pass the pagination variables as hidden values in your HTML form.

    eg - for two buttons, one of which takes you to previous page, and one to next:

    //prev button
    <form action="paginate.php" method="post">
        <input type="hidden" name="prev" value="0"/> 
        <input type="submit" value="Go to previous page"/>
    </form>
    
    //next button
    <form action="paginate.php" method="post">
        <input type="hidden" name="next" value="2"/> 
        <input type="submit" value="Go to next page"/>
    </form>
    
    //search form
    <form action="paginate.php" method="post">
        <input type="hidden" name="prev" value="0"/>         
        <input type="hidden" name="next" value="2"/> 
        <input type="text" name="search" value="User input goes here"/> 
        <input type="submit" value="Search the database"/>
    </form>
    

    The disadvantage is that POST will give 'page expired' errors when using the browser back button (but not the HTML buttons), which is not great. For that reason I'd prefer $_GET, and also because you can bookmark $_GET queries, but not $_POST.

    评论

报告相同问题?

悬赏问题

  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教