Repository navigation
Expand file tree
/
Copy pathscriptRegistro.js
More file actions
59 lines (48 loc) · 1.69 KB
/
Copy pathscriptRegistro.js
File metadata and controls
59 lines (48 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
const formR = document.getElementById("registerForm");
// Quando o botão "Cadastrar" for clicado (submit do form)
formR.addEventListener("submit", async (e) => {
e.preventDefault(); // impede o reload da página
const nome = document.getElementById("nome").value;
const email = document.getElementById("email").value;
const senha = document.getElementById("senha").value;
const tipo = document.getElementById("tipo").value;
try {
const response = await fetch("php/register.php", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ nome, email, senha, tipo }),
});
const data = await response.json();
if (data.message) {
alert(data.message);
window.location.href = "indexLogin.html"; // redireciona se deu certo
} else {
alert(data.error);
}
} catch (error) {
alert("Erro ao se registrar. Tente novamente.");
}
});
const formL = document.getElementById("loginForm");
// Quando o botão "Entrar" for clicado (submit do form)
formL.addEventListener("submit", async (e) => {
e.preventDefault(); // impede o reload da página
const email = document.getElementById("email").value;
const senha = document.getElementById("senha").value;
try {
const response = await fetch("php/login.php", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ email, senha}),
});
const data = await response.json();
if (data.message) {
alert(data.message);
window.location.href = "index.html"; // redireciona se deu certo
} else {
alert(data.error);
}
} catch (error) {
alert("Erro ao Logar. Tente novamente.");
}
});