以前的数据结果未清除

时间:2017-03-08 07:30:26

标签: javascript php jquery mysql

由于我是web programmimg的新手并且我已经对我的图书搜索页面进行了分页,最初将显示4行的第一条记录,之后当我点击下一个按钮时,下一个结果将附加到之前的结果,这样它将会再次查看结果很长。所以我需要清除以前的结果,任何人都可以帮我解决这个问题 先感谢您。 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(); 
        });

});

2 个答案:

答案 0 :(得分:1)

通常我会通过sql查询本身进行分页。限制是我使用的关键字。这样它将获得没有先前结果的新页面。 select * table_name其中id = anyid limit 5,0。接下来将是限制5,6 5,11,...并继续直到结束。 LIMIT number_of_row_to_display start_from_row_number。希望这能帮助你。

答案 1 :(得分:0)

在ajax成功之后:只需添加     $( '#显示')的html( '')。 然后     $( '#显示')HTML(结果);