使用下拉列表填充字段

时间:2019-02-05 05:57:21

标签: php html mysql

我有一个下拉列表,该列表由MySQL数据库使用以下代码填充

<?php

                    $conn = new mysqli('localhost', 'root', '@', '')
                    or die ('Cannot connect to db');

                    $result = $conn->query("select name, surname FROM clients");
                    echo "<html>";
                    echo "<body>";
                    echo "<select name='client_name' id='client'>";

                    while ($row = $result->fetch_assoc()) {

                        unset($msisdn, $contract_type, $provider);
                        $name = $row['name'];
                        $surname = $row['surname'];
                        echo '<option value="'.$name.'_'.$surname.'">'.$name.' '.$surname.'</option>';
                    }
                    echo "</select>";
                    echo "</body>";
                    echo "</html>";
                    ?>

我想用与下拉列表中的结果相关的信息填充不同的字段。

<input type="text" name="address">
<input type="text" name="tel">
<input type="text" name="id_nr">

$('#client').change(function() {
    selectedOption = $('option:selected', this);
    $('input[name=address]').val( selectedOption.data('address') );
    $('input[name=tel]').val( selectedOption.data('tel') );
    $('input[name=id_nr]').val( selectedOption.data('id_nr') );
});

1 个答案:

答案 0 :(得分:0)

您应将数据设置为选项

echo '<option data-address-"'.$address.'" value="'.$name.'_'.$surname.'">'.$name.' '.$surname.'</option>';

此外,更新查询以获取地址以及姓名和姓氏。

相关问题