问题描述
|
我想将“具有多个值的Jquery UI自动完成”应用于一个注册表单输入字段。
我要确切执行的操作:当访问者在此输入字段中键入现有用户的名称时,首先,脚本将搜索名称是否存在,并完成该名称的存在(如果存在),并添加逗号。用户可以在此字段中输入第二,第三...现有用户名,并且每次脚本都会自动完成。当访问者单击提交按钮时,PHP搜索该用户名的ID,创建ID的数组,并将其添加到db表中的新用户\“ friends \”字段中。
我的代码:
的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;
}
});
});
这是示例文件夹中的原始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);
更改了search.PHP
$conn = MysqL_connect(\"localhost\",\"user\",\"pass\");
MysqL_select_db(\"db\",$conn);
$q = strtolower($_GET[\"term\"]);
$query = MysqL_query(\"select fullname from usr_table where fullname like %$q%\");
$results = array();
while ($row = MysqL_fetch_array($query)) {
array_push($results,$row);
}
echo json_encode($results);
自动完成功能对我不起作用。找不到错误的代码部分。请帮助解决此问题。对不起,我的英语不好
解决方法
您提到要创建一个ID数组(最有可能与每个搜索到的名称相关联)吗?
在您的SQL语句中,包括您的ID字段。
$query = mysql_query(\"select id,fullname from usr_table where fullname like %$q%\");
然后,代替
array_push($results,$row)
尝试:
$results[] = array( $row[0] => $row[1] );
在使用JSON编码对其进行修改之前,它应遵循与search.php中的静态数组相同的数组结构。我不确定您的自动填充功能将抓取哪个结果作为结果输出,因此也请尝试此操作(switch row[0] with row[1]
)
$results[] = array( $row[1] => $row[0] );
编辑:
现在,我知道了JSON编码数组的外观。而是这样做:
$results[] = array ( \"id\" => $row[0],\"label\" => $row[1],\"value\" => $row[1] );
上面将数组追加到结果数组。让我知道这个是否奏效。它等效于array_push
:
array_push($results,array ( \"id\" => $row[0],\"value\" => $row[1] ));