将参数传递给 $.selector 以进行 ajax GET 请求调用。

huangapple 未分类评论54阅读模式
英文:

pass parameter to $.selector for ajax GET request call

问题

我使用循环来输出分页链接,类似这样的结构:'<< < > >>'。

在我的代码中,我通过 GET 请求设置了 $currentpage 变量:

if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
    $currentpage = (int)$_GET['currentpage'];
} else {
    $currentpage = 1;
}

$offset = ($currentpage - 1) * $rowsperpage;

我还有一个会话变量,用于保存用户在导航页面链接时选择的搜索条件。逻辑错误出现在用户点击任何分页链接时,会话变量被清除,表格显示时不带搜索条件。

经过深入研究,我发现可以使用 Ajax 来解决问题,但是我对如何实现目标感到困惑。以下是我尝试的方法,希望能得到一些帮助:

$('#pglink').click(function(e, $prevpage) {
    e.preventDefault();
    var pgNo = $prevpage;
    $.ajax({
        url: "file.php",
        type: "get",
        data: {
            currentpage: pgNo
        }
    });
});
英文:

i have pagination links echoed with a for loop like this '<< < > >>".

echo &quot;&lt;a href=&#39;{$_SERVER[&#39;PHP_SELF&#39;]}?currentpage=$prevpage&#39;&gt;&lt; &lt;/a&gt;&quot;

somewhere within my codes i set the $currentpage using GET request:

if (isset($_GET[&#39;currentpage&#39;]) &amp;&amp; is_numeric($_GET[&#39;currentpage&#39;])){
    $currentpage = (int)$_GET[&#39;currentpage&#39;];
  }else{
         $currentpage = 1;
   }
  //the offset of the list , based on current page
  $offset = ($currentpage-1) * $rowsperpage;

and i have a session variable to hold search criteria selected by the user while he navigates the page links. The logical error i get is when user clicks any of the pagination link, the session variable wipes out and table displays without the search criteria.
After intensive research i figured out i can use ajax but am confused on how to arrive my goals. THis is what i tried doing. please some help will do.

$(&#39;#pglink).click(function(e,$prevpage){//***i need to pass the current page parameter for clicked link 
e.preventDefault();
var pgNo = $prevpage;
$.ajax({
   url:&quot;file.php&quot;,
   type: &quot;get&quot;,
   data:{
   currentpage: pgNo }
   });
});

huangapple
  • 本文由 发表于 2020年5月30日 05:36:35
  • 转载请务必保留本文链接:https://java.coder-hub.com/62094919.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定