0
Good evening, I’m trying to do an input with auto complete per query in mysql database, but I’m having problems. I did all the code following a tutorial and still will not, nor errors appear to me. When I type do not appear options to complete. Follow the code:
pg index
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Auto Complete</title>
<link rel="stylesheet" href="assets/js/jquery-ui.css" />
<script src="assets/js/jquery-3.3.1.min.js"></script>
<script src="assets/js/bootstrap.min.js"></script>
<script src="assets/js/jquery-ui.js"></script>
</head>
<body>
<form action="t" method="POST" accept-charset="utf-8">
<label>Cliente:</label>
<input type="text" id="assunto" name="assunto">
</form>
<script type="text/javascript">
$(document).ready(function(){
$("#assunto").autocomplete({
source: 'retornaCliente.php'
});
});
</script>
</body>
</html>
pg return
<?php
include 'conexao.php';
$pdo = conectar();
$assunto = filter_input(INPUT_GET, 'term', FILTER_SANITIZE_STRING);
$buscar = $pdo->prepare("SELECT NOME_LOJA FROM lojas WHERE NOME_LOJA LIKE '%".$assunto."%' ORDER BY NOME_LOJA ASC ");
$buscar->execute();
while ($result = $buscar->fetch(PDO::FETCH_ASSOC)) {
$dados[] = $result['NOME_LOJA'];
}
echo json_encode($dados);
$assunto = filter_input(INPUT_GET, 'term', FILTER_SANITIZE_STRING);
//SQL para selecionar os registros
$result_msg_cont = "SELECT assunto FROM mensagens_contatos WHERE assunto LIKE '%".$assunto."%' ORDER BY assunto ASC LIMIT 7";
//Seleciona os registros
$resultado_msg_cont = $conn->prepare($result_msg_cont);
$resultado_msg_cont->execute();
while($row_msg_cont = $resultado_msg_cont->fetch(PDO::FETCH_ASSOC)){
$data[] = $row_msg_cont['assunto'];
}
echo json_encode($data)
?>
the connection with the bank is ok because I tested separately, if anyone can help me thank you.
your connection string is $Pdo or $Conn?
– user60252
Welcome to the site, be sure to mark an answer as it accepts if it solves your problem. See how in https://i.stack.Imgur.com/evLUR.png and why in https://pt.meta.stackoverflow.com/questions/1078/como-e-por-que-aceitar-uma-resposta/1079#1079
– user60252
You are returning 2 separate json, this is one of the problems. Put everything together in one array, and then generate JSON. To test, it is good to access the resource directly without AJAX, and see if the result is what you expect. Also, you should look at the PHP error log to see additional problems. There are other problems like unnecessarily recovering the subject twice, and an SQL injection problem, plus a mix of different connection variables, and lack of error handling. It would be better not to accumulate so many little problems at once so as not to delay development.
– Bacco