简体   繁体   English

如何在不更改链接的情况下在同一 php 页面上的分页链接按钮上传递 id?

[英]How to pass id on pagination link button on same php page without link change?

I want to pass value on php without refresh and link change:我想在不刷新和链接更改的情况下传递 php 的值:

$pagination .= '<li class="page-item"><a class="page-link" href="/demo-project/book?page='.$i.'">'.$i.'</a></li>';

The above code create new url what I don't want, like this: demo-project/book?page=2上面的代码创建新的 url 我不想要的东西,像这样:demo-project/book?page=2

I want When I click on link only the page id will go in php, like:我想当我点击链接时,只有页面 id 将 go 在 php 中,例如:

$pagination .= '<li class="page-item"><a class="page-link" data-id="2">'.$i.'</a></li>';

I tried this:我试过这个:

        $pagination .= '<li class="page-item"><button class="page-link" data-id="'.$i.'">'.$i.'</button></li>';

Then index.php然后索引.php

if (isset($_POST['data-id'])) {
$dataid = sanitize($_POST['data-id']);
var_dump($dataid);
}



<script>
$('button').click(function addseries(e){
var i = $(e.currentTarget).attr('data-id');
$.ajax({
  type: "POST",
 url: 'index.php',
 data: i,
 success: function(data) {
 console.log(data);
  }
});

</script>

Nothing is working I tried but this is not working.我试过没有任何效果,但这不起作用。

I want to send id and then like to get the value in php我想发送 id 然后想获取 php 中的值

but I don't want to create link href with page number in url and then get page number in php.但我不想在 url 中创建带有页码的链接 href,然后在 php 中获取页码。

Any idea or suggestions would be welcome.欢迎任何想法或建议。

You must name the parameter in you request like data: {"id":id}您必须将请求中的参数命名为 data: {"id":id}

 $('.page-item').on("click",".page-link",function(){ var i = $(this).attr('data-id'); alert(i); $.ajax({ type: "POST", url: 'index.php', data: {"data-id":i}, success: function(data){ console.log(data); } }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <li class="page-item"><button class="page-link" data-id="5">5</button></li> <li class="page-item"><button class="page-link" data-id="6">6</button></li> <li class="page-item"><button class="page-link" data-id="7">7</button></li>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM