Php 在AJAX中使用$GET分页是否可行
我正在使用$\u GET开发一个用于分页的PHP类。这是standart,从网上找到的 在这里,它工作得很好: page.php:Php 在AJAX中使用$GET分页是否可行,php,jquery,ajax,Php,Jquery,Ajax,我正在使用$\u GET开发一个用于分页的PHP类。这是standart,从网上找到的 在这里,它工作得很好: page.php: <form method ="GET"> <?php $pages = new Pagination(); echo "<br/>"; ?> </form> 我想将index.php中的page.php与ajax/jquery一起使用,并停留在index.php中 <!DOCTYPE html>
<form method ="GET">
<?php
$pages = new Pagination();
echo "<br/>";
?>
</form>
我想将index.php中的page.php与ajax/jquery一起使用,并停留在index.php中
<!DOCTYPE html>
<body>
<div id ="result"></div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script>
$(document).ready(function() {
$.post('./page.php',
function (data) {
$('#result').html(data);
}
);
});
</script>
</body>
</html>
$(文档).ready(函数(){
$.post(“./page.php”,
功能(数据){
$('#result').html(数据);
}
);
});
这可能吗 有没有可能不使用jquery的$.post,而是用$.get替换$.post?因此,不使用您所说的
$.post
,而是查找$\u get['page']
所以你可以这样做:
<script>
$(document).ready(function(e) {
var page_num = 1;
$('.nextpage').on('click',function(e){
e.preventDefault(); // stop the link from going anywhere
$.get('./page.php',
{
page: page_num // this is the same as $_GET['page']
},
function (data) {
$('#result').html(data);
page_num++;
}
);
});
$('.nextpage').click(); // emulate the click to get the first page
});
</script>
<a href="/page.php?page=2" class="nextpage">Next page</a>
是的,这确实是一个愚蠢的错误,但我想留在index.php并在page.php中操纵分页。可能我应该使用hmmm来实现这一点,所以我建议在page.php中返回一个隐藏的输入元素(div),该元素在。。然后,您可以使用新信息更新index.php分页-我将更新我的答案以反映这一点。您确定这是正确的方法吗,因为这对您的ajax响应没有多大帮助(在page.php中,因此它会检索div),如果您有一个类为
hidden_pagination
的隐藏div,它将使用从page.php
返回的类更新您的index.php
分页。。这不是您想要的吗?您可能需要稍微更改分页类,但是该方法经过了尝试和测试,因为我在过去的几个项目中使用过它。。如果你想让我用你现在拥有的东西来浏览,我们应该开始聊天
<script>
$(document).ready(function(e) {
$('.pagination').on('click','a',function(e){
e.preventDefault(); // stop the link from going anywhere
var next_page = $(this).attr('data-id'); // get the next page from the link data-id attribute
$.get('./page.php',
{
page: next_page // this is the same as $_GET['page']
},
function (data) {
$('#result').html(data);
$('.pagination').html($('#result').children('.hidden_pagination').html()); // add the new pagination to the current pagination
}
);
});
$('.nextpage').click(); // emulate the click to get the first page
});
</script>
<div class="pagination">
<a href="#" class="nextpage" data-id="2">Next page</a>
</div>
<div id="result">
this will be replaced with the ajax response
</div>