1

我有一个来自 JQUERY 的自动完成功能,我想要产品的 ID 而不是我选择的产品的名称。

这是完整的自动完成:

    <?php

    // if the 'term' variable is not sent with the request, exit
    if ( !isset($_REQUEST['term']) )
    exit;

    // connect to the database server and select the appropriate database for use
    include 'conexion2.php';

    // query the database table for client codes that match 'term'
    $rs = mysql_query('SELECT id_cliente, nombrecliente FROM cliente where nombrecliente like "'. mysql_real_escape_string($_REQUEST['term']) .'%" order by nombrecliente asc', $conexio);

    // loop through each name returned and format the response for jQuery
$data = array();
    if ( $rs && mysql_num_rows($rs) )
    {
    while( $row = mysql_fetch_array($rs, MYSQL_ASSOC) )
    {
        $data[] = array(
            'label' => $row['nombrecliente'],
            'value' => $row['nombrecliente'],
        );
    }
    }

    // jQuery wants JSON data
    echo json_encode($data);
    flush();

当我选择客户时,我不知道如何取出身份证。如果我更改 'value' => $row['id_cliente'],那么我会在我的自动完成 INPUT 中获得 ID 号....

4

1 回答 1

1

在您的数组中,添加另一个项目:

'id' => $row['id_cliente']

然后,您需要添加一个隐藏的表单字段来存储您的 ID。在您的 jQuery 中(假设您使用的是 jQueryUI 自动完成功能),您可以在源代码下方添加它作为自动完成功能的一部分:

$('#autocomplete_input').autocomplete({
    source: '/path/to/lookup/php/file',
    select: function(event, ui){
        $('#my_hidden_field').val(ui.item.id);
    }
});

然后,当您在提交后阅读表单时,您将阅读该 ID 而不是自动完成的字段。

于 2012-10-31T16:05:20.603 回答