![](/img/trans.png)
[英]Jquery autocomplete two fields based on one field using PHP and MySQL
[英]jQuery Autocomplete PHP Mysql posting different field
我正在使用jQuery的自动完成功能,在其中使用名为site.php的php文件填充自动完成功能下拉列表。 Site.php从名为site的mysql表中获取值,该表具有3列: id
, code
和site
。 我希望我的自动填充功能仅显示code
和site
,然后将相应的id
存储在其他表中。
除了自动完成功能会发布code
和选定的site
而不是id
之外,其他所有功能都可以正常运行。 为了将id
发送到我的php POST,而不发送code
和site
我需要更改什么? 脚本如下:
PHP文件:site.php
<?php
$server = 'sql203.com';
$user = 'xxxxxxxxxxxx';
$password = 'xxxxxxx';
$database = 'b17';
$mysqli = new MySQLi($server,$user,$password,$database);
/* Connect to database and set charset to UTF-8 */
if($mysqli->connect_error) {
echo 'Database connection failed...' . 'Error: ' . $mysqli->connect_errno . ' ' . $mysqli->connect_error;
exit;
} else {
$mysqli->set_charset('utf8');
}
/* retrieve the search term that autocomplete sends */
$term = trim(strip_tags($_GET['term']));
$a_json = array();
$a_json_row = array();
if ($data = $mysqli->query("SELECT * FROM `b17_16413362_upupa`.`site` WHERE code LIKE '%$term%' OR site LIKE '%$term%' ORDER BY code , site")) {
while($row = mysqli_fetch_array($data)) {
$id = htmlentities(stripslashes($row['id']));
$code = htmlentities(stripslashes($row['code']));
$site = htmlentities(stripslashes($row['site']));
$a_json_row["id"] = $id;
$a_json_row["value"] = $code.' '.$site;
$a_json_row["label"] = $code.' '.$site;
array_push($a_json, $a_json_row);
}
}
// jQuery wants JSON data
echo json_encode($a_json);
flush();
$mysqli->close();
?>
Javascript:
<script type="text/javascript">
$(function() {
$("#sitex").autocomplete({
source: 'site.php',
minLength: 0
}).focus(function(){
$(this).autocomplete("search");
});
});
</script>
在您的PHP中,更改
$a_json_row["value"] = $code.' '.$site;
至
$a_json_row["value"] = $id;
“值”属性是表单将提交的数据。 “标签”属性将显示给用户。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.