doufen9815 2015-09-05 23:52
浏览 57
已采纳

jQuery UI自动完成多个远程(JSON,PHP,JS)

I want to make a jQuery UI Autocomplete with multiple values and remote (https://jqueryui.com/autocomplete/#multiple-remote).

Now I have these codes:

JS

$(function() {
    function split( val ) {
      return val.split( /,\s*/ );
    }
    function extractLast( term ) {
      return split( term ).pop();
    }

    $("#retr")
        .bind( "keydown", function( event ) {
            if ( event.keyCode === $.ui.keyCode.TAB &&
                $( this ).autocomplete( "instance" ).menu.active ) {
                event.preventDefault();
            }
        })
        .autocomplete({
            source: function( request, response ) {
                $.getJSON( "retr/retr_arry.php", {
                term: extractLast( request.term )
            }, response );
        },
        search: function() {
            var term = extractLast( this.value );
            if ( term.length < 3 ) {
                return false;
            }
        },
        focus: function() {
            return false;
        },
        select: function( event, ui ) {
            var terms = split( this.value );
            terms.pop();
            terms.push( ui.item.value );
            terms.push( "" );
            this.value = terms.join( ", " );
            return false;
        }
    });
});

HTML

<input id="retr" type="text" placeholder="Please select the user(s)">

PHP

//Database connect...

if (isset($_POST["term"])) {
    $input_term = $_POST["term"];
} else {
    exit; 
}
$username = array();
$term2 = '%'.$term.'%';

//SQL
$sql = $db->prepare("SELECT username FROM users WHERE username LIKE ?");
$sql->bind_param('s', $term2);
if ($sql->execute()) {
    $sql->store_result();
    $sql->bind_result($username);
    while ($sql->fetch()) {
        $usernames[] = $username;
    }
    echo (json_encode($usernames));
} else {
    exit;
}

The Problem

I get no autocomplete. When I enter up to 5 letters I still get no results.

Edit

After changing POST to GET (in the PHP file), I get results. But the results aren't sorted correctly.

For example: If I enter "adm" I get "SYSTEM". How can I fix this? I think the problem is in the SQL part of the PHP.

  • 写回答

1条回答 默认 最新

  • dpntq48842 2015-09-06 00:30
    关注
    if (array_key_exists("term", $_POST)) {
        $term2 = '%'.$_POST["term"].'%';
    } else {
        exit;
    }
    $username = array();
    
    //SQL
    $sql = $db->prepare("SELECT username FROM users WHERE username LIKE ?");
    $sql->bind_param('s', $term2);
    if ($sql->execute()) {
        $sql->store_result();
        $sql->bind_result($username);
        while ($sql->fetch()) {
            $usernames[] = $username;
        }
        echo json_encode($usernames);
    } else {
        exit;
    }
    

    instead of use exit... better return an error message / return the proper HTTP State, so you can handle this cases instead of dropping them and crash your javascript.


    Update: An hint: instead of using mysqli use PDO. PDO is forward-looking and supports many database driver. so it's easy to switch them and so on. PDO is easier and your code could be smaler and better to understand.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。