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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器