简体   繁体   English

自动填充功能无法正常运行

[英]Autocomplete doesn't work properly

I want to apply "Jquery UI Autocomplete with multiple values" to one registration forms input field. 我想将“具有多个值的Jquery UI自动完成”应用于一个注册表单输入字段。

What i want to do exactly: When visitor types name of existing user to this input field, first of all, script searches for name existence, completes it (if exists), adds comma. 我想要做的事情:当访问者为此输入字段键入现有用户的名称时,首先,脚本搜索名称存在,完成它(如果存在),添加逗号。 User can type second, third... existing usernames to this field, and everytime script will auto complete. 用户可以在此字段中键入第二个,第三个...现有用户名,并且每次脚本都将自动完成。 And when visitor clicks to submit button, PHP searches for id's of this usernames, creates array of id's, adds it to new users "friends" field in db table. 当访问者点击提交按钮时,PHP搜索此用户名的id,创建id的数组,将其添加到db表中的新用户“friends”字段。

My code: 我的代码:

HTML HTML

<form action="index.php" method="post">      
<input class="std" type="text" name="friends"  id="friends"/>
<input type="submit" name="submit">
</form>

Jquery jQuery的

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

    $( "#friends" )
        // don't navigate away from the field on tab when selecting an item
        .bind( "keydown", function( event ) {
            if ( event.keyCode === $.ui.keyCode.TAB &&
                    $( this ).data( "autocomplete" ).menu.active ) {
                event.preventDefault();
            }
        })
        .autocomplete({
            source: function( request, response ) {
                $.getJSON( "search.php", {
                    term: extractLast( request.term )
                }, response );
            },
            search: function() {
                // custom minLength
                var term = extractLast( this.value );
                if ( term.length < 2 ) {
                    return false;
                }
            },
            focus: function() {
                // prevent value inserted on focus
                return false;
            },
            select: function( event, ui ) {
                var terms = split( this.value );
                // remove the current input
                terms.pop();
                // add the selected item
                terms.push( ui.item.value );
                // add placeholder to get the comma-and-space at the end
                terms.push( "" );
                this.value = terms.join( ", " );
                return false;
            }
        });
});

this is original php file from sample folder which works perfectly. 这是来自示例文件夹的原始php文件,它完美地运行。 but i want to fetch from database instead of array 但我想从数据库而不是数组中获取
Original search.php 原始search.php

$q = strtolower($_GET["term"]);
if (!$q) return;
$items = array(
"Great Bittern"=>"Botaurus stellaris",
"Little Grebe"=>"Tachybaptus ruficollis",
"Black-necked Grebe"=>"Podiceps nigricollis",
"Little Bittern"=>"Ixobrychus minutus",
"Black-crowned Night Heron"=>"Nycticorax nycticorax",
"Purple Heron"=>"Ardea purpurea",
"White Stork"=>"Ciconia ciconia",
"Spoonbill"=>"Platalea leucorodia",
"Red-crested Pochard"=>"Netta rufina",
"Common Eider"=>"Somateria mollissima",
"Red Kite"=>"Milvus milvus",

);

function array_to_json( $array ){

    if( !is_array( $array ) ){
        return false;
    }

    $associative = count( array_diff( array_keys($array), array_keys( array_keys( $array )) ));
    if( $associative ){

        $construct = array();
        foreach( $array as $key => $value ){

            // We first copy each key/value pair into a staging array,
            // formatting each key and value properly as we go.

            // Format the key:
            if( is_numeric($key) ){
                $key = "key_$key";
            }
            $key = "\"".addslashes($key)."\"";

            // Format the value:
            if( is_array( $value )){
                $value = array_to_json( $value );
            } else if( !is_numeric( $value ) || is_string( $value ) ){
                $value = "\"".addslashes($value)."\"";
            }

            // Add to staging array:
            $construct[] = "$key: $value";
        }

        // Then we collapse the staging array into the JSON form:
        $result = "{ " . implode( ", ", $construct ) . " }";

    } else { // If the array is a vector (not associative):

        $construct = array();
        foreach( $array as $value ){

            // Format the value:
            if( is_array( $value )){
                $value = array_to_json( $value );
            } else if( !is_numeric( $value ) || is_string( $value ) ){
                $value = "'".addslashes($value)."'";
            }

            // Add to staging array:
            $construct[] = $value;
        }

        // Then we collapse the staging array into the JSON form:
        $result = "[ " . implode( ", ", $construct ) . " ]";
    }

    return $result;
}

$result = array();
foreach ($items as $key=>$value) {
    if (strpos(strtolower($key), $q) !== false) {
        array_push($result, array("id"=>$value, "label"=>$key, "value" => strip_tags($key)));
    }
    if (count($result) > 11)
        break;
}
echo array_to_json($result);

Changed search.php 改变了search.php

$conn = mysql_connect("localhost", "tural", "0579ural")  or die( mysql_error() );;
mysql_select_db("askon", $conn)  or die( mysql_error() );;
$q = strtolower($_GET["term"]);
if (!$q) return;
$query = mysql_query("select id, fullname from usr_table where fullname like '$q%'")  or die( mysql_error() );;
$results = array();
while ($row = mysql_fetch_array($query)) {
   $results[] = array( $row[1] => $row[0] );
}
echo json_encode($results);

Autocomplete php script works prntscr.com/22mxl but i think something wrong with jquery: it doesn't show menu. 自动完成PHP脚本工作prntscr.com/22mxl但我认为jquery有问题:它不显示菜单。 prntscr.com/22mxg . prntscr.com/22mxg How to solve this issue? 如何解决这个问题? PS FOR ORIGINAL SEARCH.PHP IT RETURNS something like this prntscr.com/22n0e and shows prntscr.com/22n0r . PS for ORIGINAL SEARCH.PHP返回类似这样的prntscr.com/22n0e并显示prntscr.com/22n0r

SemanticScuttle does what you want: SemanticScuttle做你想要的:

Have a look at the (working) first file, it should give you enough hints to get yours working. 看看(工作)第一个文件,它应该给你足够的提示让你的工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM