我正在尝试在我的网页上创建一个实时搜索栏,但似乎无法使其正常工作。它只是不显示任何数据

时间:2017-10-27 16:47:02

标签: javascript php ajax livesearch

template.php文件包含与脚本相关的所有编码。

<?php
include 'dbh.php';
?>
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->

<html>
    <head>
        <meta charset="UTF-8">
        <title><?php echo $title; ?></title>
        <link rel ='Stylesheet' type ='text/css' href ='Styles/StyleSheet.css'/>

    </head>
    <body>

            <div id="banner">

            </div>
            <form action="">
                <input type ="text" id="search">

            </form>
        <div id="result">

        </div>
            <div id="content_area">
                <?php echo $content; ?>

            </div>
        <script type="text/javascript" src="jquery.js"></script>
        <script type="text/javascript">
        $(document).ready(function()
        {
            $('#search').keyup(function()
            {
                var search = $('#search').val();
                if($.trim(search.length)==0)
                {
                    $('#result').html('Please enter correct data');
                }
                else
                {
                    $.ajax({
                                type:'POST',
                                url:'Search.php',
                                data:{'search':search},
                                success:function(data)
                                {
                                   $('#result').html(data); 
                                }
                            });
                }
            })
        })


        </script>

    </body>
</html>

Search.php文件包含与数据查询相关的代码。我的数据库只有一个名为CINFO的表,只有一个名为Title的列。

<?php
include 'dbh.php';
if(isset($_POST['search']))
{
   $search = $_POST['search'];
   $query = $db->prepare("SELECT * from CINFO where TITLE LIKE ?");
   $query->execute(["%$search%"]);
   $count = $query->rowCount();
   if($count==0)
   {
       echo "Sorry No result found";
   }
   else
   {

       while($r = $query->fetch(PDO::FETCH_OBJ))
       {
           $name = $r->Title;
           echo "$name";
       }
   }
}

?>

dbh.php包含与数据库连接相关的代码 -

<?php
$server = "localhost";
$username = "root";
$password = "root";
$dbname = "ComicDB";
$conn = mysqli_connect($server, $username, $password,$dbname);
?>

每当我在搜索框中输入任何数据时,它都不显示任何数据,但当我按退格键并删除我输入的内容时,它会显示“请输入正确的数据”,这意味着模板中的if块。 php正在执行但不是else块。这就是我从代码中理解的东西。似乎无法弄清楚我做错了什么。

0 个答案:

没有答案