diff --git a/controllers/getUsers.php b/controllers/getUsers.php new file mode 100644 index 0000000..93ec9e1 --- /dev/null +++ b/controllers/getUsers.php @@ -0,0 +1,39 @@ +getConnection(); + // echo""; + + return $conn; + + } catch (PDOException $e) { + // $client->trackException($e); + error_log("Error connecting to SQL Server: " . $e->getMessage()); + die("Error connecting to SQL Server."); + } +} + +function getUsers(){ + $conn = createDatabaseConnection(); + $query = "SELECT * FROM usuario"; + $stms = $conn->query($query); + $stms->execute(); + + $results = $stms->fetchAll(PDO::FETCH_ASSOC); + return $results; +} + +if ($_SERVER['REQUEST_METHOD'] == 'GET') { + // echo""; + $responseArray = getUsers(); + + echo json_encode($responseArray); +} \ No newline at end of file diff --git a/js/consultaUser.js b/js/consultaUser.js new file mode 100644 index 0000000..20fb8cb --- /dev/null +++ b/js/consultaUser.js @@ -0,0 +1,26 @@ +//Variáveis +const userTableBody = document.getElementById('user-table-body'); + +// Event Listeners +document.addEventListener('DOMContentLoaded', () => { + buscaUsers(); +}) + +const buscaUsers = async () => { + const response = await fetch('../controllers/getUsers.php', { + method: 'GET' + }); + + if (!response.ok) + throw new Error("Error"); + + const data = await response.json(); + data.reverse(); + console.log(data); + + data.forEach((item)=>{ + var row = userTableBody.insertRow(); + row.insertCell().textContent = item.nome; + row.insertCell().textContent = item.email; + }); +} \ No newline at end of file diff --git a/pages/consulta_usr.html b/pages/consulta_usr.html index f5e8f39..a6eada3 100644 --- a/pages/consulta_usr.html +++ b/pages/consulta_usr.html @@ -63,56 +63,15 @@