DIDS-Coursework/DIS-COMP4039-CW2-psxrp11-20450011/psxrp11-20450011_InstallationFiles/js/login.js

35 lines
856 B
JavaScript

// Login stuff
document.addEventListener("DOMContentLoaded", () =>
{
fetch("isLoggedIn.php").then(res => res.json().then(json =>
{
if (json.message === "ok")
{
window.location.href = "./search.html";
}
}));
});
document.querySelector("#login").addEventListener("submit", e =>
{
e.preventDefault();
let formData = new FormData();
formData.append("username", document.querySelector("#username").value);
formData.append("password", document.querySelector("#password").value);
fetch("login.php", {
method: "POST",
body: formData
}).then(res => res.json().then(json =>
{
if (json.message === "ok")
{
window.location.href = "search.html";
}
else
{
alert(json.message);
}
}));
});