gpt4 book ai didi

php - Jquery UI 自动完成

转载 作者:行者123 更新时间:2023-12-01 05:05:26 26 4
gpt4 key购买 nike

我想将“具有多个值的 Jquery UI 自动完成”应用到一个注册表单输入字段。

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

我的代码:

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

这对我不起作用。请帮忙

最佳答案

据我所知,自动完成功能正在标签数据中搜索。

$query = mysql_query("select fullname as label from usr_table where fullname like %$q%");

更新:

尝试firebug用于调试。您可以在那里跟踪 ajax 通信。

关于php - Jquery UI 自动完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6369624/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com