jquery ui自动完成html表单的多输入类型和mysql


jquery ui autocomplete html form multiple input type and mysql

我有一个具有多个输入类型的表单
我知道如何使用外部源文件(在我的情况下是PHP)从mysql数据库中检索数据,但我不知道如何在不重复代码的情况下将mysql数据库的每一列关联到正确的输入字段
请帮帮我…

HTML:

<form id="form" class="form" autocomplete="off" action="" method="POST">
    <input class="auto" type="text" name="client" value="" id="client" placeholder="Client Name" autofocus />
    <input class="auto" type="text" name="firstname" value="" id="firstname" placeholder="First Name" />
    <input class="auto" type="text" name="lastname" value="" id="lastname" placeholder="Last Name" />
</form>

PHP:

<?php
header('Content-Type: text/html; charset=utf-8'); 
include_once '../connection.php';
if (isset($_GET['term'])) {
    $return_arr = [];
    $query = ("SELECT DISTINCT client, FirstName, LastName
               FROM office_adv 
               WHERE client LIKE :term OR
                     FirstName LIKE :term OR
                     LastName LIKE :term");
    $sth = $db->prepare($query);
    $sth->execute(array('term' => '%' .$_GET['term'] . '%'));
    while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
        $return_arr[] = $row['client'];
        $return_arr[] = $row['FirstName'];
        $return_arr[] = $row['LastName'];
    }
    echo json_encode($return_arr, JSON_UNESCAPED_UNICODE);
}
?>  

JQUERY:

$(function() {
    $(".auto").autocomplete({
        source: "php/clientFind.php",
        minLength: 2,
        autoFocus: true
    });
});  

再次感谢!!

如果您希望每个输入te都有相同的自动完成列表,那么您做得很好。

如果您希望每个输入都有一个特定的自动完成列表,则必须对每个输入应用不同的autocomplete()方法调用,并在数据库中选择正确的数据。

如果您想用一行中与手动填写的输入匹配的数据预填充所有字段,您可能误解了自动完成的功能。