我有这个自动完成的 php 函数:
搜索.php
if ( !isset($_REQUEST['term']) )
exit;
$dblink = mysql_connect('localhost', 'root', '') or die( mysql_error() );
mysql_select_db('inb');
$rs = mysql_query('select name, l_name, level from pacient
where name like "'. mysql_real_escape_string($_REQUEST['term']) .'%"
or l_name like "'. mysql_real_escape_string($_REQUEST['term']) .'%"
order by name asc limit 0,10', $dblink);
$data = array();
if ( $rs && mysql_num_rows($rs) )
{
while( $row = mysql_fetch_array($rs, MYSQL_ASSOC) )
{
$nivel = $row['nivel_nivel'];
$data[] = array(
'label' => $row['name'].' '. $row['l_name'].' '.$row['level'],
'value' => $row['name'].' '. $row['l_name'].' '.$row['level']
);
}
}
echo json_encode($data);
flush();
?>
我将其调用到 html 主文件中:
jQuery(document).ready(function(){
$('#nameField').autocomplete({source:'search.php', minLength:1});
});
现在,我想获取在 php 文件中进行的查询的值,并将其放入 html 文件的输入字段中。知道我该怎么做吗?
最佳答案
您可以为 response
设置监听器JS 自动完成定义中的方法。您需要更改 PHP 以在该数据集中的某处包含您想要的新值。
像这样:
$data[] = array(
'label' => $row['name'].' '. $row['l_name'].' '.$row['level'],
'value' => $row['name'].' '. $row['l_name'].' '.$row['level'],
'foo' => <WHATEVER VALUE YOU WANT HERE>
);
然后在 jQuery 中:
$('#nameField').autocomplete({
source:'search.php',
minLength:1,
response : function(event, data) {
data.content[0].foo; //this will have your foo value, do whatever you want with it here
}
});
关于php - 将查询变量发送到 php 函数的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15510336/