59 lines
1.5 KiB
PHP
59 lines
1.5 KiB
PHP
<?php
|
|
session_start(); // Iniciar sessão
|
|
|
|
// Verificar se o usuário está logado
|
|
if (!isset($_SESSION['user_id'])) {
|
|
http_response_code(401); // Não autorizado
|
|
echo json_encode([]);
|
|
exit();
|
|
}
|
|
|
|
header('Content-Type: application/json; charset=utf-8');
|
|
|
|
// Conectar ao banco de dados
|
|
$mysqli = new mysqli("10.15.0.42", "root", "34641747", "valida");
|
|
|
|
// Verificar erros na conexão
|
|
if ($mysqli->connect_error) {
|
|
echo json_encode([]);
|
|
exit();
|
|
}
|
|
|
|
// Obter o termo de pesquisa
|
|
$term = isset($_GET['term']) ? trim($_GET['term']) : '';
|
|
|
|
if ($term === '') {
|
|
echo json_encode([]);
|
|
exit();
|
|
}
|
|
|
|
// Preparar a consulta para buscar nomes, CPFs ou emails que correspondam ao termo
|
|
$stmt = $mysqli->prepare("
|
|
SELECT id, name, cpf, email
|
|
FROM clients
|
|
WHERE name LIKE CONCAT('%', ?, '%')
|
|
OR cpf LIKE CONCAT('%', ?, '%')
|
|
OR email LIKE CONCAT('%', ?, '%')
|
|
LIMIT 10
|
|
");
|
|
$stmt->bind_param("sss", $term, $term, $term);
|
|
$stmt->execute();
|
|
$result = $stmt->get_result();
|
|
|
|
// Construir o array de sugestões
|
|
$suggestions = [];
|
|
while ($row = $result->fetch_assoc()) {
|
|
$suggestions[] = [
|
|
'id' => htmlspecialchars($row['id'], ENT_QUOTES, 'UTF-8'),
|
|
'label' => htmlspecialchars($row['name'] . ' - ' . $row['cpf'] . ' - ' . $row['email'], ENT_QUOTES, 'UTF-8'),
|
|
'value' => htmlspecialchars($row['name'], ENT_QUOTES, 'UTF-8') // Valor exibido no input
|
|
];
|
|
}
|
|
|
|
echo json_encode($suggestions);
|
|
|
|
// Fechar o statement e a conexão
|
|
$stmt->close();
|
|
$mysqli->close();
|
|
?>
|