Php Jquery自动完成,

时间:2017-11-19 13:31:34

标签: php jquery

我在主页面中有自动完成输入和[添加按钮]。 按添加按钮,用户重定向以添加新记录。 保存新记录后,用户将重定向回主页。

我想,当用户被重定向到主页时,输入字段已填充此添加的记录数据

如何做到这一点?

这是我的代码:

HTML

<div class="form-group">
  <label>Sender <span class="text-danger">*</span></label>
  <input type="text" id="sender_name" name="sender_name" required="required" class="form-control" placeholder="Type to select" value="">
  <input type="hidden" id="sender_id" name="sender_id">
</div>

AJAX

    $(function() {
        $( "#sender_name" ).autocomplete({
            source: 'add_document_sender_autocomplete.php',
            minLength: 1, 

            change: function (event, ui) {
                if (!ui.item) {
                    $(this).val("");
                }
            },

            select: function(event, ui) {

                $('#sender_id').val(ui.item.id);

            }


        });
    });

QUERY

//get search term
$searchTerm = $_GET['term'];
//get matched data from skills table

$query = $mysqli->query("SELECT sender_id, sender_vat_tax_code, sender_name, sender_foreign_name FROM rm_senders WHERE replace(replace(replace(sender_vat_tax_code, '.', ''), '-', ''), '/', '')  LIKE '%".$searchTerm."%' OR concat_ws(' ', sender_name, sender_foreign_name) LIKE '%".$searchTerm."%' ORDER BY sender_name ASC");

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

$data[] = array (
 'id' => $row['sender_id'],
 'label' => '('.$row['sender_vat_tax_code'].') '. $row['sender_name'].' '.$row['sender_foreign_name'],
);

}

//return json data
echo json_encode($data);

1 个答案:

答案 0 :(得分:0)

首先,您必须导入此js库

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.devbridge-autocomplete/1.2.24/jquery.autocomplete.min.js"></script>
<link rel="stylesheet" type="text/css" href="//cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css">
<script src="//cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>

HTML

<div class="form-group">
    <label>Sender <span class="text-danger">*</span></label>
    <input type="text" id="sender_name" name="sender_name" required="required" class="form-control"
           placeholder="Type to select" value="">
    <input type="hidden" id="sender_id" name="sender_id">
</div>

AJAX

$(function () {
    $("#sender_name").autocomplete({
        source: 'add_document_sender_autocomplete.php',
        select: function (event, ui) {
            var sel = ui.item;
            $('#sender_id').val(sel.id);
        },
        minLength: 1,
        autoFocus: false,
        delay: 250,
        response: function (event, ui) {
            if (ui.content.length == 1 && ui.content[0].id != 0) {
                ui.item = ui.content[0];
                $(this).val(ui.item.value);
                $(this).removeClass('ui-autocomplete-loading');
            }
        },
    });
});

QUERY

$return_arr = array();

$tget = "%" . $_GET['term'] . "%";
$query = $mysqli->query("SELECT sender_id, sender_vat_tax_code, sender_name, sender_foreign_name FROM rm_senders WHERE replace(replace(replace(sender_vat_tax_code, '.', ''), '-', ''), '/', '')  LIKE '%" . $searchTerm . "%' OR concat_ws(' ', sender_name, sender_foreign_name) LIKE '%" . $searchTerm . "%' ORDER BY sender_name ASC");
//print_r($stock);
while ($row = $query->fetch_assoc()) {
    $row_array['id'] = $row->sender_id;
    $row_array['label'] = '(' . $row['sender_vat_tax_code'] . ') ' . $row['sender_name'] . ' ' . $row['sender_foreign_name'];
    array_push($return_arr, $row_array);
}
die(json_encode($return_arr));