1

我有一些监控表单的 JQuery。基本上,对于每个 keyup,它都会调用一个 php 文件来搜索数据库。

$(document).ready(function() {
    $("#faq_search_input").watermark("Begin Typing to Search");

    $("#faq_search_input").keyup(function() {
        var faq_search_input = $(this).val();
        var dataString = 'keyword='+ faq_search_input;

        if (faq_search_input.length > 2) {
            $.ajax({
                type: "GET",
                url: "core/functions/searchdata.php",
                data: dataString,
                beforeSend: function() {
                    $('input#faq_search_input').addClass('loading');
                },
                success: function(server_response) {
                    $('#searchresultdata').empty();
                    $('#searchresultdata').append(server_response);
                    $('span#faq_category_title').html(faq_search_input);
                }
            });
        }

        return false;
    });
});

这很好用,但是它会#searchresultdata根据查询过滤结果。唯一的事情是,如果表单中没有任何内容,我希望它加载所有内容 - 用户不必单击表单来执行此操作,因此 a.blur不起作用。

PHP 文件很简单:

if(isset($_GET['keyword'])){}
4

2 回答 2

1

您应该在您的服务器上处理 [*] 搜索

$query = "SELECT Image, Manufacturer, Model FROM Device_tbl WHERE Manufacturer LIKE '%$keyword%' OR Model LIKE '%$keyword%";
if ($keyword=='*') $query = "SELECT Image, Manufacturer, Model FROM Device_tbl";

$(document).ready(function() {
    $("#faq_search_input").watermark("Begin Typing to Search");

    $("#faq_search_input").keyup(function() {
        var faq_search_input = $(this).val();
        if (faq_search_input =='') faq_search_input ='*';
        var dataString = 'keyword='+ faq_search_input;

        if (faq_search_input.length > 2 || faq_search_input=='*') {
            $.ajax({
                type: "GET",
                url: "core/functions/searchdata.php",
                data: dataString,
                beforeSend: function() {
                    $('input#faq_search_input').addClass('loading');
                },
                success: function(server_response) {
                    $('#searchresultdata').empty();
                    $('#searchresultdata').append(server_response);
                    $('span#faq_category_title').html(faq_search_input);
                }
            });
        }

        return false;
    });
    $("#faq_search_input").trigger('keyup');
});
于 2012-05-08T10:12:01.370 回答
1

如果您最初加载所有结果,那么您是否可以不将其存储在 JavaScript 数组中并使用 JavaScript 过滤结果?这将在每次按键时为您节省一个 HTTP 请求,这只会有利于您网站的速度和资源使用。

编辑:样本。

<?php
    $sql = "SELECT `title` FROM `your_table`";
    $res = mysql_query($sql);
    $rows = array();
    while ($row = mysql_fetch_assoc($res)) {
        $rows[] = $row['title'];
    }
    echo '<script>var data = ' . json_encode($rows) . ';</script>';
?>
<form method="post" action="">
  <fieldset>
    <input type="text" name="search" id="faq_search_input" />
  </fieldset>
</form>
<script>
    // I presume you're using jQuery
    var searchInput = $('#faq_search_input');
    var searchResults = $('#searchresultdata');
    var tmpArray = data;

    // add all results to results div
    $.each(data, function(key, val) {
        searchResults.append('<li>' + val + '</li>');
    });

    searchInput.attr('placeholder', 'Begin typing to search');
    searchInput.keyup(function() {
        // hide any <li> in your #searchresultdata that don't match input
    });
</script>

我不知道你的serverresponse变量中有什么,所以我只能猜测放入searchresultdata <div>. 您还需要修改 SQL 查询以匹配您的表名和列名。

searchdata.php 的内容

$query = "SELECT Image, Manufacturer, Model FROM Device_tbl WHERE Manufacturer LIKE '%$keyword%' OR Model LIKE '%$keyword%'";
if ($keyword=='*') $query = "SELECT Image, Manufacturer, Model FROM Device_tbl";
$result=mysql_query($query, $database_connection) or die(mysql_error());

if($result){
    if(mysql_affected_rows($database_connection)!=0){
          while($row = mysql_fetch_object($result)){
?>
     <div class="hold-cont">
        <div class="holder">
            <div class="image-hold" >
                <img class="image-icon" src="<? echo $deviceimg.($row->Image); ?>"/>
</div>
        </div>
        <div class="device-name devicename-txt"><? echo($row->Manufacturer. ' ' .$row->Model);  ?></div>
    </div>
     <?
    }
    }else {
        echo 'No Results for :"'.$_GET['keyword'].'"';
    }

}
}else {
    echo 'Parameter Missing';
}
于 2012-05-08T10:31:10.490 回答