因爲我是web programmimg的新手,我已經完成了分頁到我的圖書搜索頁面,最初的第4行記錄將顯示,此後當我點擊下一個按鈕時,下一個結果將追加到以前的結果,以便再次查看結果冗長。所以我需要清除以前的結果,任何一個可以幫助我解決bug請 謝謝提前。 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(event)';>1</a>
<a href="#" onclick='pagination(event)';>2</a>
<a href="#" onclick='pagination(event)';>3</a>
<a href="#" onclick='pagination(event)';>4</a>
</form>
</div>
<script type="text/javascript" src="js/page.js">
</script>
</body>
</html>
ajax.php
<?php
include('db.php');
$num_rec_per_page = 5;
?>
<div id="navigation">
<?php
if(isset($_POST['author']))
{
$author=mysql_real_escape_string($_POST['author']);
if($author=="")
{
echo "Please Enter Title or Author or Publisher";
}
else
{
$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%'");
$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']);
$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)
{
alert("next");
var TotalPages=Math.ceil(no_of_records/items_per_page);
page++;
var start_page=page;
var end_page=start_page*5;
var start_page=end_page;
var end_page=start_page*5;
alert(start_page);
alert(end_page);
var author=$("#author").val();
e.preventDefault();
$.ajax({
type: "POST",
url: "db/ajax.php",
data: {'start':start_page,'end':end_page,'page':page,'author':author},
cache: false,
success: function(result){
//alert("submitted"+result);
$('#display').html(result);
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}
});
}
function pagination(e)
{
page++;
var start_page=page;
var end_page=start_page*5;
alert(start_page);
alert(end_page);
var author=$("#author").val();
e.preventDefault();
$.ajax({
type: "POST",
url: "db/ajax.php",
data: {'start':start_page,'end':end_page,'page':page,'author':author},
cache: false,
success: function(result){
//alert("submitted"+result);
$('#display').html(result);
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}
});
}
function Previous(e)
{
page--;
var start_page=page;
var end_page=start_page*5;
var page=end_page;
alert(start_page);
alert(end_page);
var TotalPages=Math.ceil(no_of_records/items_per_page);
var author=$("#author").val();
e.preventDefault();
$.ajax({
type: "POST",
url: "db/ajax.php",
data: {'start':start_page,'end':end_page,'page':page,'author':author},
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;
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();
});
});
還是得到了以前的結果 – Vradhit
對不起附加..... – Vradhit