I can`t find the way to get the ID of my element when you search in the autocomplete textbox: I have this in mi HTML:
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script>
$(function() {
$( "#local" ).autocomplete({
source: 'buscar_local_nombre.php'
});
});
And this is "buscar_local_nombre.php":
<?php
$dbHost = 'localhost';
$dbUsername = 'root';
$dbPassword = '';
$dbName = 'restofinder';
//connect with the database
$db = new mysqli($dbHost,$dbUsername,$dbPassword,$dbName);
//get search term
$searchTerm = $_GET['term'];
//get matched data from table
$query = $db->query("SELECT * FROM locales WHERE NOMBRE LIKE '%".$searchTerm."%' AND ESTADO = 1 ORDER BY NOMBRE ASC");
while ($row = $query->fetch_assoc()) {
$data[] = $row['NOMBRE'];
}
//return json data
echo json_encode($data);
?>
How could I get ID too (hidden) so that i can store it in a variable and use it afterwards?
Thank you very much!
答案 0 :(得分:1)
Use select
$(function() {
$( "#local" ).autocomplete({
source: 'buscar_local_nombre.php',
select: function (event, ui) {
$("#hdnId").val(ui.item.id);//Put Id in a hidden field
}
});
});
Reference