0

我正在使用 $_GET 开发一个用于分页的 PHP 类。它是标准的,从网上找到的。

在这里效果很好:page.php:

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

这是可能的方式吗?

4

2 回答 2

1

是否有可能不使用 jquery 的 $.post,而是用 $.get 替换 $.post?

于 2013-04-25T12:59:57.223 回答
1

所以而不是$.post像你说的那样寻找$_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>

值得注意的是,在你page.php身上你不需要那个表格,因为我看不到它会做很多事情

更新

因此,要在index.phpfrom上操作分页,page.php您可以page.php返回一个隐藏的 div.hidden_pagination及其全部内容。

<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>
于 2013-04-25T13:03:04.427 回答