2013-04-25 67 views
0

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

这工作好: page.php文件:

<form method ="GET"> 
<?php 
$pages = new Pagination(); 
echo "<br/>"; 
?> 
</form> 

我想在index.php来使用这个page.php文件中的index.php与阿贾克斯/ jQuery和住

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

这可能吗?

回答

1

是否有可能不使用jQuery的$ .post,您可以用$ .get替换$ .post?

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你不需要那样的表格,因为我看不到它会做很多事情

UPDATE

所以有操作上index.php分页从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> 
+0

是的,这确实是一个愚蠢的错误,但我想留在AT index.php并操纵page.php中的分页。也许我应该这样做