将查询的变量发送到php函数的输入

Sending a variable of a query to an input from a php function

本文关键字:php 函数 输入 查询 变量      更新时间:2023-09-26

我有这个自动完成php函数:

search.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文件的输入字段中。知道怎么做吗?

您可以在JS自动完成定义中为response方法设置侦听器。您需要更改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
    }
});