我打算对我的搜索页面进行分页,因为我总共有16条记录,所以我分为4页,每页包含4个记录。我使用javascript来处理分页。我使用了ajax请求JavaScript的。但我的问题是,当我点击script.js的提交按钮时,前四条记录会显示,但是当我点击下一个按钮时,它显示任何东西,请你告诉我哪里出错我需要做什么修正,请, 先感谢您。 book_search.php
<?php
include('assets/page_header.php');
?>
<script type="text/javascript" src="js/page.js">
</script>
<div class="container">
<h1>SEARCHING THE BOOK</h1>
<form id="search" name="search" action="#" method="post">
Search : <input type="text" name="author" id="author">
<input id="submit" name="submit" type="submit" value="Submit">
<div id="display">
</div>
<a href='#' onclick='Next(event)'; >Next</a>
<a href='#' onclick='Previous(event)'; >Previous</a>
<a href='#' onclick='Back(event)'; >back</a>
<a href='#' onclick='LastPage(event)';>LastPage</a>
<a href="#" onclick='pagination()';>1</a>
<a href="#" onclick='pagination()';>2</a>
<a href="#" onclick='pagination()';>3</a>
<a href="#" onclick='pagination()';>4</a>
</form>
</div>
</body>
</html>
ajax.php
<?php
include('db.php');
$page="";
if(isset($_GET['page']))
{
$page=$_GET['page'];
echo $page;
}
$num_rec_per_page = 5;
?>
<div id="navigation">
<
<?php
if(isset($_POST['author']))//&&isset($_POST['page'])&&isset($_POST['total']))
{
$author=mysql_real_escape_string($_POST['author']);
$total_pages=mysql_real_escape_string($_POST['total']);
if($author=="")
{
echo "Please Enter Title or Author or Publisher";
}
else
{
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * $num_rec_per_page;
$query1="select * from books where title LIKE '%$author%' OR author LIKE '%$author%' OR publisher LIKE '%$author%' LIMIT $start_from, $num_rec_per_page ";
echo $query1;
$rs_result=mysql_query("select * from books where title LIKE '%$author%' OR author LIKE '%$author%' OR publisher LIKE '%$author%'");
$total_records = mysql_num_rows($rs_result);
echo $total_records;//count number of records
$total_pages = ceil($total_records / $num_rec_per_page);
$phpself=$_SERVER['PHP_SELF'];
if($page>1)
{
$pagenumber=$page-1;
$prev="<a href=\"$phpself?page=$pagenumber\">[Back]</a>";
$first="<a href=\"$phpself?page=1\">[FirstPage]</a>";
}
else
{
$prev='';
$first='';
}
if($page<$total_pages)
{
$pagenumber=$page+1;
$next="<a id='pagin' 'page=$page' >[Next]</a>";
$last="<a id='pagin' 'total=$total_pages'>[LastPage]</a>";
}
else
{
$next="";
$last="";
/*$next='[next]';
$last='[LastPage]';*/
}
//for ($i=1; $i<=$total_pages; $i++) {
// echo "<a href='members1.php?page=".$i."'> ".$i."</a> ";
// };
echo $first.$prev."Showing page<bold>$page</bold>of<bold>$total_pages</bold>pages".$next.$last;
/*if($total_records > 0) {
echo "<a href='members1.php?page=1'> ".'<'." </a> "; // Goto 1st page
}
// echo "<a href='members1.php?page=1'> ".'<<'." </a> ";
if($total_records > 0) {
echo "<a href='members1.php?page=$total_pages'> ".'>'." </a> "; // Goto last page
}*/
$result1=mysql_query($query1) or die(mysql_error());
//print_r($result1);
$count=mysql_num_rows($result1);
//echo $count;
$display= "<table align='center'>";
$display.= "<tr><td>title</td> <td>author</td> <td>publisher</td> <td>numcopies</td> <td>status</td> <td>number_of_copies_available</td> <td>Action</td> </tr>";
while($row=mysql_fetch_array($result1)){
$count=mysql_num_rows($result1);
//print_r($row['bookid']);
$r12=$row['bookid'];
$query2=mysql_query("select bookid from bookrentalinfo where bookid=$r12");
$num_copies_borrowed=mysql_num_rows($query2);
$num_copies_count=$row['numcopies'];
$number_of_copies_available=$num_copies_count-$num_copies_borrowed;
$display.= "<tr>";
//echo "<td>".$row['bookid']."</td>";
$display.="<td>".$row['title']."</td>";
$display.= "<td>".$row['author']."</td>";
$display.= "<td>".$row['publisher']."</td>";
$display.= "<td>".$row['numcopies']."</td>";
$display.= "<td>".$row['status']."</td>";
$display.= "<td>".$number_of_copies_available."</td>";
if($number_of_copies_available>0)
{
$display.= "<td><a href='borrow_search.php?book_id=".$row['bookid']."'>Rent</a></td>";
}
else {
$display.= "rent link is not activated";
$display.="<td></td>";
}
$display.= "</tr>";
}
$display.="</table>";
echo $display;
}
}
?>
</div>
<?php
if(isset($_POST['start'])&& isset($_POST['end'])&&isset($_POST['author']))
{
echo "hi";
$author=mysql_real_escape_string($_POST['author']);
$start_val=mysql_real_escape_string($_POST['start']);
$end_val=mysql_real_escape_string($_POST['end']);
$start_val=$page;
$end_val=$start_val*5;
$string=" select * from books where title LIKE '%$author%' OR author LIKE '%$author%' OR publisher LIKE '%$author%' LIMIT $start_val, $end_val ";
echo $string;
$query20=mysql_query($string);
echo $query20;
$display= "<table align='center'>";
$display.= "<tr><td>title</td> <td>author</td> <td>publisher</td> <td>numcopies</td> <td>status</td> <td>number_of_copies_available</td> <td>Action</td> </tr>";
while($row=mysql_fetch_array($query20)){
$count=mysql_num_rows($query20);
//print_r($row['bookid']);
$r12=$row['bookid'];
$query2=mysql_query("select bookid from bookrentalinfo where bookid=$r12");
$num_copies_borrowed=mysql_num_rows($query2);
$num_copies_count=$row['numcopies'];
$number_of_copies_available=$num_copies_count-$num_copies_borrowed;
$display.= "<tr>";
//echo "<td>".$row['bookid']."</td>";
$display.="<td>".$row['title']."</td>";
$display.= "<td>".$row['author']."</td>";
$display.= "<td>".$row['publisher']."</td>";
$display.= "<td>".$row['numcopies']."</td>";
$display.= "<td>".$row['status']."</td>";
$display.= "<td>".$number_of_copies_available."</td>";
if($number_of_copies_available>0)
{
$display.= "<td><a href='borrow_search.php?book_id=".$row['bookid']."'>Rent</a></td>";
}
else {
$display.= "rent link is not activated";
$display.="<td></td>";
}
$display.= "</tr>";
}
$display.="</table>";
echo $display;
}
?>
page.js
var no_of_records=16;
var items_per_page=4;
var page=1;
var start_page=1;
var end_page=0;
function Next(e)
{
var start_page=page;
var end_page=start_page*5;
var TotalPages=Math.ceil(no_of_records/items_per_page);
page++;
e.preventDefault();
$.ajax({
type: "POST",
url: "db/ajax.php",
data: {'start':start_page,'end':end_page,'page':page},
cache: false,
success: function(result){
//alert("submitted"+result);
$('#display').html(result);
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}
});
}
function pagination()
{
e.preventDefault();
$.ajax({
type: "POST",
url: "db/ajax.php",
data: {'start':start_page,'end':end_page,'page':page},
cache: false,
success: function(result){
//alert("submitted"+result);
$('#display').html(result);
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}
});
}
function Previous(e)
{
var start_page=page;
var end_page=start_page*5;
var TotalPages=Math.ceil(no_of_records/items_per_page);
page--;
e.preventDefault();
$.ajax({
type: "POST",
url: "db/ajax.php",
data: {'start':start_page,'end':end_page,'page':page},
cache: false,
success: function(result){
//alert("submitted"+result);
$('#display').html(result);
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}
});
}
script.js
$(document).ready(function(){
$("#submit").click(function(e){
var author = $("#author").val();
var dataString='author='+author;
alert(dataString);
if(author=='')
{
alert("Please Enter Author or Title or Publisher Fields");
}
else
{
// AJAX Code To Submit Form.
$.ajax({
type: "POST",
url: "db/ajax.php",
data: dataString,
cache: false,
success: function(result){
//alert("submitted"+result);
$('#display').html(result);
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}
});
}
e.preventDefault();
});
});