PHP - 分页:$ _POST或$ _GET

时间:2014-10-19 02:05:09

标签: javascript php jquery pagination

我正在开发一个使用php分页的搜索系统(多个过滤器)。 起初我使用POST方法和主窗体。但是,使用POST我无法保持分页。原因:当用户按名称搜索时,例如,当他在下一页中单击时,查询会丢失并返回到第一页。 为了解决这个问题,我改用了GET方法。 但是使用GET,url获取参数。并且用户不希望这样。

示例:

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

我只是想成为

http://mysearch.com/search.php

我尝试了这种解决方法:

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

但是当我点击浏览器中的“上一页/后一页”时,带有参数的URL也会返回。

有解决方案吗?使用GET或POST和分页,参数不会显示在URL?

1 个答案:

答案 0 :(得分:0)

您可以像使用$ _GET一样使用$ _POST进行分页,而无需使用SESSION变量/ cookie,您只需将分页变量作为隐藏值传递到HTML表单中。

例如 - 对于两个按钮,其中一个按钮带您到上一页,一个到下一个按钮:

//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>

缺点是POST会使页面过期&#39;使用浏览器后退按钮(但不是HTML按钮)时出错,这不是很好。出于这个原因,我更喜欢$ _GET,还因为你可以为$ _GET查询添加书签,但不是$ _POST。

相关问题