感谢您的回答!
我正在开发一个由前同事制作的现有网站。
有一个 php 页面可以从数据库中获取数据
我会解释我想要什么:
我有一个带有 .autocomplete jquery 的输入文本,但我想在下拉列表中使用数据库表获取的数据来更改它:
<li class="field-container"><label for="pr_code">
<span class="field-name"><?php echo $LANG['product_code']; ?></span>
<input type="text" value="" id="pr_code" name="pr_code" class="uiStyle text ui-widget-content ui-corner-all" onFocus="showHideTip(this);"> *</label>
<div id="pr_codeTip" class="hidden formTip">
<?php echo $LANG['purchase_pr_code_tip']; ?> </div>
</li>
如何使用获取的数据更改下拉列表中的输入文本?
这里是为 .autocomplete 获取数据的 pr.php 代码
<?php require_once("st_inc/session.php"); ?>
<?php confirm_logged_in(); ?>
<?php require_once("st_inc/connection.php"); ?>
<?php require_once("st_inc/functions.php"); ?>
<?php
if(!isset($_GET["term"])) { redirect_to("home.php"); }
$q = strtolower($_GET["term"]);
if (!$q) return;
$query = "SELECT * FROM products";
$results = mysql_query($query, $connection);
confirm_query($results);
$items = array();
while($row = mysql_fetch_array($results)) {
$items[$row['pr_code']] = $row['pr_name'];
}
/*
$items = array();
while($row = mysql_fetch_array($results)) {
$items[] = $row['pr_name'];
}
echo json_encode($items);
*/
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);
?>
非常感谢