2011-06-16 168 views
2

我想在一個註冊表單輸入字段中應用「帶有多個值的JQuery UI自動完成」。 我想要做什麼:當訪客鍵入現有用戶的名字到這個輸入字段時,首先,腳本搜索名字存在,完成它(如果存在),添加逗號。用戶可以輸入第二,第三...現有的用戶名到這個字段,每次腳本都會自動完成。當訪問者點擊提交按鈕時,PHP搜索這個用戶名的id,創建id的數組,將它添加到db表中的新用戶「friends」字段。幫我解決jQuery和PHP的問題

我的代碼:

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來實現它 。代碼i 的哪個部分必須更改?

對不起,我的壞 英語

回答