dongwei1855 2017-01-29 04:02
浏览 65
已采纳

jQuery自动完成功能根据我的输入词从数据库中提取项目,但结果显示为列表的水平线

The jQuery autocomplete function brings the item from the database based on my typing word but result shows as just horizontal line of list. Attached image is showing how the result looks like.

Result

HTML:

<form class="navbar-seach pull-center">
    <div>
        <input type="text" placeholder="Search Productstyle="width:550px; height:40px;padding-left: 10px;margin-top: 20px;" id="searchBox" onkeyup="if (event.keyCode === 13) {searchResults(this.value); this.value = '';}">
    </div>
</form>

PHP:

include 'searchbox.php';

$switch_id = filter_input(INPUT_POST, 'switch_id', FILTER_SANITIZE_SPECIAL_CHARS);

if (isset($_POST['name_startsWith']))
    $searchWordResults = $_POST['name_startsWith'];
else
    $searchWordResults = null;

switch($switch_id){
    case 1:
        $userObj = new searchBox();
        echo $userObj->autoComplete($searchWordResults);
        break;
}

jQuery:

$("#searchBox").autocomplete({
    source: function(request, response) {
        $.ajax({
            type: "POST",  
            url: "class/searchbox_switch.php",
            dataType: "json",
            data: { name_startsWith: request.term, switch_id: 1 },
            success: function(data) {
                response($.map(data, function(item){
                    return {
                        label: item.label,
                        value: item.value
                    };
                }));
            },
            error: function(response) {
                console.log(response.responseText);
            }
        });
    },
    autoFocus: true,
    minLength: 1,
    select: function( event, ui ) {
        searchResults(ui.item.label);
    }
});

PHP (searchbox.php):

include '../dbConnect.php';

class searchBox {
    public function autoComplete($name_startsWith) {
        $letterSearch = $name_startsWith . "%";
        $connectObj = new db_connect();
        $dbh = $connectObj->connect();

        $stmt = $dbh->prepare("SELECT product_name FROM products WHERE product_name LIKE :start");
        $stnd = array();
        $stmt->bindParam(':start', $letterSearch, PDO::PARAM_STR);
        $stmt->execute();
        $stnd = $stmt->fetchAll(PDO::FETCH_ASSOC);
        $json = json_encode($stnd);
        return $json;
    }

}
  • 写回答

1条回答 默认 最新

  • dtrvzd1171 2017-01-31 06:58
    关注

    OK got it. You are passing wrong key in your result. Your result data key is "product_name" And You are trying to access "label ". Try This solution:

     success: function( data ) {                
              response( $.map( data, function(item) {                    
                 return {
                       label: item.product_name,
                 }
              }));                    
    
     },
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA