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(); ?>