I have a form with multiple input type.
I know how retrieve data from mysql database using an external source file (in my case it's PHP) but i don't know how to associate each column from mysql database to the right input field without repeating the code??
Please help me..
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
});
});
thanks again !!
If you want each input te have the same autocomplete list, you are doing it well.
If you want a specific autocomplete list for each input, you have to apply a different autocomplete()
method call to each input and select the correct data in your database.
If you want to pre fill all fields with data from one row that match an input you filled manually, you may have misunderstood what autocomplete do.