【问题标题】:Help me to solve problem with jQuery and PHP帮我解决 jQuery 和 PHP 的问题
【发布时间】:2011-09-16 13:51:55
【问题描述】:

我想将“具有多个值的 Jquery UI 自动完成”应用于一个注册表单输入字段。 我想要做什么:当访问者在此输入字段中键入现有用户的名称时,首先,脚本搜索名称存在,完成它(如果存在),添加逗号。用户可以在此字段中键入第二个、第三个...现有用户名,并且每次脚本都会自动完成。当访问者单击提交按钮时,PHP 搜索此用户名的 id,创建 id 数组,将其添加到 db 表中的新用户“朋友”字段中。

我的代码:

HTML

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

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;
            }
        });
});

search.php

$conn = mysql_connect("localhost", "user", "pass")  or die( mysql_error() );;
mysql_select_db("db", $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 ( "id" => $row[0] , "label" => $row[1], "value" => $row[1] );}
echo json_encode($results);

我的问题:

  1. 我只需要书面姓名的 id, 和全名。我不需要价值。 我怎样才能从jquery中删除它 代码?
  2. 如何获取 id 的数组 的书面名称,并放在 usr_table 之后的“朋友”字段 提交注册表?
  3. 最后我想实现它 与 mysqli。我的代码的哪些部分 必须改变吗?

对不起我的不好 英文

【问题讨论】:

    标签: php jquery mysql autocomplete


    【解决方案1】:

    这个自动完成器可以做你想做的事:

    http://www.devbridge.com/projects/autocomplete/jquery/#demo

    请尝试键入以逗号分隔的国家/地区名称。我在我的网页上使用它并且效果很好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-29
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-18
      • 2023-03-30
      • 1970-01-01
      相关资源
      最近更新 更多