PHP:使用Ajax无法正常工作
我正在从数据库中提取记录,并在使用php和ajax的gruops中显示它们。PHP:使用Ajax无法正常工作
下面的脚本(找到某处)几乎可以工作,但似乎当您在第一页上并单击“下一步”时,您将被带到最后一页。
只有单独单击数字时,分页才是正确的。
如果需要,我可以在PM中提供测试链接。
的index.php:
<script>
$(document).ready(function() {
$("#results").load("pages.php"); //load initial records
//executes code below when user click on pagination links
$("#results").on("click", ".pagination a", function (e){
e.preventDefault();
var page = $(this).attr("data-page"); //get page number from link
$("#results").load("pages.php",{"page":page}, function(){ //get content from PHP page
});
});
});
</script>
<div id="results"></div>
pages.php:
<?php
// continue only if $_POST is set and it is a Ajax request
if (isset($_POST) && isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
include (realpath(__DIR__ . '/../db.php'));
// Get page number from Ajax POST
if (isset($_POST["page"])) {
$page_number = filter_var($_POST["page"], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH); //filter number
if (!is_numeric($page_number)) {
die('Invalid page number!');
}
}
else {
$page_number = 1; //if there's no page number, set it to 1
}
// get total number of records from database for pagination
$sql = "SELECT * FROM " . $DBtable . " ORDER BY dates DESC";
$rs = $conn->query($sql);
if ($rs === false) {
trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $conn->error, E_USER_ERROR);
}
else {
$get_total_rows = $rs->fetch_row(); //hold total records in variable
}
// break records into pages
$total_pages = ceil($get_total_rows[0]/$item_per_page);
// get starting position to fetch the records
$page_position = (($page_number - 1) * $item_per_page);
// Limit our results within a specified range.
$rs = $conn->prepare("SELECT id, title, description, dates, clicks FROM " . $DBtable . " ORDER BY dates DESC LIMIT $page_position, $item_per_page");
$rs->execute(); //Execute prepared Query
$rs->bind_result($id, $title, $description, $dates, $clicks); //bind variables to prepared statement
echo '<ul class="contents">';
while($rs->fetch()){ //fetch values
echo '<li>';
echo $id. '. <strong>' .$name.'</strong> — '.$description;
echo '</li>';
}
echo '</ul>';
echo '<div align="center">';
/* We call the pagination function here to generate Pagination link for us. */
echo paginate_function($item_per_page, $page_number, $get_total_rows[0], $total_pages);
echo '</div>';
exit;
}
// ############### pagination function #########################################
function paginate_function($item_per_page, $current_page, $total_records, $total_pages) {
$pagination = '';
if ($total_pages > 0 && $total_pages != 1 && $current_page <= $total_pages) { //verify total pages and current page number
$pagination.= '<ul class="pagination">';
$right_links = $current_page + 3;
$previous = $current_page - 3; //previous link
$next = $current_page + 1; //next link
$first_link = true; //boolean var to decide our first link
if ($current_page > 1) {
$previous_link = ($previous == 0) ? 1 : $previous;
$pagination.= '<li class="class="page-item" first"><a class="page-link" href="#" data-page="1" title="First">«</a></li>'; //first link
$pagination.= '<li class="page-item"><a class="page-link" href="#" data-page="' . $previous_link . '" title="Prev"><</a></li>'; //previous link
for ($i = ($current_page - 2); $i < $current_page; $i++) { //Create left-hand side links
if ($i > 0) {
$pagination.= '<li class="page-item"><a class="page-link" href="#" data-page="' . $i . '" title="Page' . $i . '">' . $i . '</a></li>';
}
}
//set first link to false
$first_link = false;
}
if ($first_link) { //if current active page is first link
$pagination.= '<li class="page-item active first"><a class="page-link" href="#">' . $current_page . '<span class="sr-only">(current)</span></a></li>';
}
elseif ($current_page == $total_pages) { //if it's the last active link
$pagination.= '<li class="page-item active last"><a class="page-link" href="#">' . $current_page . '<span class="sr-only">(current)</span></a></li>';
}
else { //regular current link
$pagination.= '<li class="page-item active"><a class="page-link" href="#">' . $current_page . '<span class="sr-only">(current)</span></a></li>';
}
for ($i = $current_page + 1; $i < $right_links; $i++) { //create right-hand side links
if ($i <= $total_pages) {
$pagination.= '<li class="page-item"><a class="page-link" href="#" data-page="' . $i . '" title="Page ' . $i . '">' . $i . '</a></li>';
}
}
if ($current_page < $total_pages) {
$next_link = ($i > $total_pages) ? $total_pages : $i;
$pagination.= '<li class="page-item"><a class="page-link" href="#" data-page="' . $next_link . '" title="Next">></a></li>'; //next link
$pagination.= '<li class="page-item last"><a class="page-link" href="#" data-page="' . $total_pages . '" title="Last">»</a></li>'; //last link
}
$pagination.= '</ul>';
}
//return pagination links
return $pagination;
}
// Free memory
$rs->free();
// Close connection
$conn->close();
?>
这是寻呼机的电流解析的HTML:
<ul class="pagination">
<li class="page-item active first"><a class="page-link" href="#">1<span class="sr-only">(current)</span></a></li>
<li class="page-item"><a class="page-link" href="#" data-page="2" title="Page 2">2</a></li>
<li class="page-item"><a class="page-link" href="#" data-page="3" title="Page 3">3</a></li>
<li class="page-item"><a class="page-link" href="#" data-page="4" title="Next">></a></li>
<li class="page-item last"><a class="page-link" href="#" data-page="4" title="Last">»</a></li>
</ul>
只是尝试下面一样在你的paginate_function()
$ next_link =($ I> $ TOTAL_PAGES)? $ total_pages:$ i;
到
$ next_link =(($ CURRENT_PAGE + 1)> $ TOTAL_PAGES)? $ total_pages:$ current_page + 1;
这是有效的。谢谢!但似乎我与Previous链接有相同的问题。我应该怎样改变那个? – Meek
$ previous = $ current_page - 3; //以前的链接替换为$ previous = $ current_page - 1; –
也批准答案,如果你有你的解决方案,所以其他用户将快速得到解决方案 –
请在paginate_function功能的变化低于
if ($current_page < $total_pages) {
$next_link = ($i > $total_pages) ? $total_pages : $i;
$pagination.= '<li class="page-item"><a class="page-link" href="#" data-page="' . $next_link . '" title="Next">></a></li>'; //next link
$pagination.= '<li class="page-item last"><a class="page-link" href="#" data-page="' . $total_pages . '" title="Last">»</a></li>'; //last link
}
更改为
if ($current_page < $total_pages) {
$next_link = ($i > $total_pages) ? $total_pages : $next;
$pagination.= '<li class="page-item"><a class="page-link" href="#" data-page="' . $next_link . '" title="Next">></a></li>'; //next link
$pagination.= '<li class="page-item last"><a class="page-link" href="#" data-page="' . $total_pages . '" title="Last">»</a></li>'; //last link
}
这将是有用的OP和其他可能会读这个问题来解释你已经改变了什么,为什么 – RiggsFolly
@Danish Husain>谢谢。你能更清楚一点吗? – Meek
发布你的分页html,所以我想法什么你传递下一个和前一个按钮点击 –