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

51 lines
1.3 KiB
JavaScript

// create new a user stuff
document.addEventListener("DOMContentLoaded", () =>
{
fetch("isAdmin.php").then(res => res.json().then(json =>
{
if (json.message !== "ok")
{
window.location.href = "./search.html";
}
}));
});
document.querySelector("#createUser").addEventListener("submit", e =>
{
e.preventDefault();
let username = document.querySelector("#username").value;
let password = document.querySelector("#pass").value;
let rePass = document.querySelector("#rePass").value;
let admin = document.querySelector("#isAdmin").checked;
if (password === "" || rePass === "" || username === "")
{
alert("Please fill in all fields");
}
else if (password !== rePass)
{
alert("Passwords do not match");
}
else
{
let data = new FormData();
data.append("username", username);
data.append("password", password);
data.append("admin", admin);
fetch("newUser.php", {
method: "POST",
body: data
}).then(res => res.json().then(json =>
{
if (json.message === "ok")
{
alert("User created");
}
else
{
alert("Error creating user");
}
}));
}
});