Created
February 16, 2021 02:16
-
-
Save LucianoCharlesdeSouza/7bfe42f198af8958e9f6d9c49c1e2245 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>AutoComplete</title> | |
</head> | |
<body> | |
<input type="text" list="mysearch" name="search" placeholder="pesquise aqui..."> | |
<datalist id="mysearch"> | |
</datalist> | |
<script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script> | |
<script> | |
$(function() { | |
$('input[name=search]').keyup(function() { | |
let inputValue = $(this).val(); | |
$.ajax({ | |
url: 'http://localhost/data-list/conexao.php', | |
method: 'POST', | |
data: { | |
nome: inputValue | |
}, | |
dataType: 'json', | |
success: function(response) { | |
let html = ''; | |
for (usuario of response) { | |
html += `<option value="${usuario.nome}" data-id="${usuario.id}">`; | |
} | |
$('#mysearch').html(html); | |
} | |
}); | |
var optionSelected = $(`#mysearch option[value=${inputValue}]`)[0] | |
if (optionSelected) { | |
console.log(optionSelected.dataset.id) | |
} | |
}) | |
}) | |
</script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
try { | |
$options = [PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ]; | |
$pdo = new PDO('mysql:host=localhost;dbname=clinica', 'root', '', $options); | |
} catch (PDOException $e) { | |
var_dump($e->getMessage()); | |
} | |
$nome = filter_input(INPUT_POST, 'nome'); | |
$sql = "SELECT * FROM usuarios WHERE nome LIKE :nome"; | |
$stmt = $pdo->prepare($sql); | |
$stmt->bindValue(':nome', "%{$nome}%"); | |
$stmt->execute(); | |
$result = $stmt->fetchAll(); | |
if (count($result) > 0) { | |
echo json_encode($result); | |
exit; | |
} | |
echo json_encode([]); | |
exit; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment