1

JQUERY からのオートコンプリートがあり、選択している製品の名前ではなく、製品の ID が必要です。

これは完全な AUTOCOMPLETE です。

    <?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();

クライアントを選択する際に ID を取り出す方法がわかりません。'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 に答える