帮我解决 jQuery 和 PHP 的问题

发布于 2024-11-15 13:36:46 字数 2814 浏览 0 评论 0原文

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

我的代码:

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 的“friends”字段之后 提交登记表?
  3. 最后我想意识到这一点 与 mysqli.代码的哪些部分我 必须改变吗?

抱歉我的不好 英语

I want to apply "Jquery UI Autocomplete with multiple values" to one registration forms input field.
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.

My code:

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

My questions:

  1. I need only id of written name,
    and fullname. I don't need value.
    How can i delete it from jquery
    code?
  2. how to fetch array of id's
    of written names, and put to
    "friends" field of usr_table after
    submission of registration form?
  3. And finally i want to realise it
    with mysqli. Which parts of code i
    must change?

Sorry for my bad
english

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

偷得浮生 2024-11-22 13:36:46

此自动完成程序可以执行您想要的操作:

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

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

This autocompleter can do what you want:

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

Just try to type country names separated by commas. I use it on my webpages and works well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文