Jump to content

Search support incident data is overwritten


phpVietHa

Recommended Posts

I have a questions like this. Now click on the button once I get search keywords entered data into it. Then I want is a new keyword search with the old data must be destroyed and new data show. Please do help me sir!

process with php :

bookModel

<?php
    require_once 'database.php';
    class Books
    {
        var $conn;
        public function __construct()
        {
            $this->conn = new Database();
        }
        // cac phuong thuc ghi o day
        public function getBookList()
        {
            $sql = "SELECT * FROM booklist order by bookid DESC";
            $result = mysql_query($sql);
            $data = array();
            while($rows = mysql_fetch_assoc($result))
            {
                $data[] = $rows;
            }
            return $data;
        }
        // viet function tim kiem thong tin voi keyword nhap vao
        public function searchBooks($keyword)
        {
            $sql = "SELECT * FROM booklist WHERE bookName like '%$keyword%' ORDER BY bookID desc";
            $result = mysql_query($sql);
            $data = array();
            while($rows = mysql_fetch_assoc($result))
            {
                $data[] = $rows;
            }
            return $data;
        }
    }
?>

process get data php

<?php
    require 'bookModel.php';
    $bookList = new Books();       
    $keyWord = $_POST['textSearch']; // tu khoa tim kiem nhap ben phia client
    // gio ta se tim kiem trong dong du lieu vua roi 
    $data = $bookList->searchBooks($keyWord);                    
    $jsonString = json_encode($data);
    print_r($jsonString);
?>

Search html

<!DOCTYPE HTML>
<html>
    <head>
        <script type="text/javascript" src="jquery-1.10.2.js"></script>
        <script type="text/javascript">
            $("document").ready(function(){
                var dem = 0; // chua click thi i = 0
                $("#Search").click(function(){
                    var url = "newWind.php";
                    var dta = {
                        "textSearch": $("#searchForm :text[name='txtSearch']").val()
                    };
                    $.post(url,dta,function(data,status)                                       
                    {
                        console.log(data);
                        console.log(status);
                        var html = "<table border='1'";
                        $.each(data,function(key,value){
                            html += "<tr>";
                                html += "<td>" +value["bookID"] +"</td>";
                                html += "<td>" +value["bookName"] +"</td>";                                
                                html += "<td>" +value["bookPrice"] +"</td>";
                            html += "</tr>"; 
                        });
                        html += "</table>";
                        $("#Result").append(html);                                            
                    },'json');
                    dem += 1; // tang i len 1
                    if(dem > 1)
                    {
                        $("#Result").remove();                        
                    }                    
                }); 
            });
        </script>
    </head>
    <body>
        <!-- thiet ke giao dien tim kiem -->
        <form action="" id="searchForm">
            <label>Input keyword:</label>
            <input type="text" name="txtSearch"/>
            <br />
            <input type="button" name="btnSearch" id="Search" value="Search"/>
        </form>
        <div id="Result">
                        
        </div>          
    </body>
</html>
 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.