即时搜索不起作用

时间:2015-08-29 01:03:45

标签: javascript php jquery html

以下代码用于即时搜索,例如谷歌搜索。

请注意,我在此处未包含连接数据库功能。因为我认为数据库用户名和密码设置会与你的不同。所以如果你想测试它,请创建你自己的。 mysql设置了一个名为“objects”的表,它有一个名为“name”的列。

每当我输入任何内容时,会弹出另一个新的搜索框,我期待搜索结果。

有人可以帮忙请!我坚持了将近一天,需要它明天工作..提前感谢!!

<!-- display the search area -->
<html>
<!-- google API reference -->
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<!-- my own script for search function -->

<center>
<form method="POST">
    <input type="text" name="search" style="width:400px " placeholder="Search box" onkeydown="searchq();">
    <input type="submit" value=">>">
    <div id="output">
    </div>
</form>
</center>
</html>

<?php
$output="";
if(isset($_POST["searchVal"])){
    //get the search
    $search=$_POST["searchVal"];
    //sort the search
    $search=preg_replace("#[^0-9a-z]#i","",$search);
    //query the search
    $query=mysqli_query($conn,"SELECT * from objects WHERE name LIKE '%$search%'") or die("could not search!");
    $count=mysqli_num_rows($query);
    //sort the result
    if($count==0){
        $output="there was no search result";
    }
    else{
        while($row=mysqli_fetch_assoc($query)){

            $object_name=$row["name"];

            $output.="<div>".$object_name."</div>";
        }
    }
}


?>
  <!-- instant search function -->
 <script type="text/javascript">
function searchq(){
    // get the value
    var searchTxt = $("input[name='search']").val();
    // post the value
    $.post("search.php",{searchVal: searchTxt},function(output){
        $("#output").html(output);
    });
}

</script>

1 个答案:

答案 0 :(得分:0)

您需要将onkeydown事件更改为onkeyup事件,因为onkeydown事件不会捕获在搜索结果中按下的当前键。我修改了你的代码,如下所示。请尝试:

HTML代码:

<html>
<!-- google API reference -->
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<!-- my own script for search function -->

<center>
<form method="POST">
    <input type="text" name="search" style="width:400px " placeholder="Search box" onkeyup="searchq();">
    <input type="submit" value=">>">
    <div id="output">
    </div>
</form>
</center>   

  <!-- instant search function -->
 <script type="text/javascript">
function searchq(){
    // get the value
    var searchTxt = $("input[name='search']").val();
    // post the value
    if(searchTxt != "")
    {
        $.post("search.php",{searchVal: searchTxt},function(output){
            $("#output").html(output);
        });
    }
    else
    {
        $("#output").html("");
    }
}
</script>
</html>

PHP文件(search.php)

<?php
print_r($_POST);
$output="";
if(isset($_POST["searchVal"])){
    //get the search
    $search=$_POST["searchVal"];
    //sort the search
    $search=preg_replace("#[^0-9a-z]#i","",$search);
    //query the search
    echo "<br/>SELECT * from objects WHERE name LIKE '%$search%'<br/>";
    $query=mysqli_query($conn,"SELECT * from objects WHERE name LIKE '%$search%'") or die("could not search!");
    $count=mysqli_num_rows($query);
    //sort the result
    if($count==0){
        $output="there was no search result";
    }
    else{
        while($row=mysqli_fetch_assoc($query)){

            $object_name=$row["name"];

            $output.="<div>".$object_name."</div>";
        }
    }
    echo $output;
}
?>

我正在打印传递给search.php的内容和在db中触发的查询以进行调试。如果需要进一步的帮助,请告诉我。