Php jQueryUI自动完成
我想将“Jquery UI多值自动完成”应用于一个注册表输入字段 我想做的是:当访问者在这个输入字段中输入现有用户的名称时,首先,脚本搜索名称存在,完成它(如果存在),添加逗号。用户可以键入第二、第三。。。将现有用户名添加到此字段,并且每次脚本都将自动完成。当访问者点击提交按钮时,PHP搜索该用户名的id,创建id数组,将其添加到db表中的新用户“friends”字段中 我的代码: HTMLPhp jQueryUI自动完成,php,jquery,autocomplete,jquery-ui-autocomplete,Php,Jquery,Autocomplete,Jquery Ui Autocomplete,我想将“Jquery UI多值自动完成”应用于一个注册表输入字段 我想做的是:当访问者在这个输入字段中输入现有用户的名称时,首先,脚本搜索名称存在,完成它(如果存在),添加逗号。用户可以键入第二、第三。。。将现有用户名添加到此字段,并且每次脚本都将自动完成。当访问者点击提交按钮时,PHP搜索该用户名的id,创建id数组,将其添加到db表中的新用户“friends”字段中 我的代码: HTML <form action="index.php" method="post">
<form action="index.php" method="post">
<input class="std" type="text" name="friends" id="friends"/>
<input type="submit" name="submit">
</form>
已更改的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);
$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);
这对我不起作用。据我所知,AutoComplete正在标签数据中搜索,请提供帮助
$query = mysql_query("select fullname as label from usr_table where fullname like %$q%");
更新:
尝试调试。您可以在那里进行ajax通信 你可能想描述什么对你不起作用……我描述了我的想法。php脚本不工作。为了实现我的想法,我必须做些什么?Offtopic:我建议在将其放入查询字符串之前先传递
$q
。非常感谢您的回复。你明白我的想法吗?我更改了代码,但没有成功这是源于示例文件夹的原始php文件(更新的问题。请再次阅读),它运行良好。但我想从数据库而不是数组中获取
$query = mysql_query("select fullname as label from usr_table where fullname like %$q%");