Currently, I am learning the basics of HTML and focusing on tasks such as login/logout functionality, creating users, and deleting users (only permitted when logged in as an admin).
For updating a user password, I have utilized PUT
, for creating a user account POST
is used, and to logout DELETE
is employed.
My challenge now is figuring out how to delete a user account while logged in as an admin, which is something I haven't been able to crack yet.
This marks my third attempt at seeking help here, recognizing that my explanation may not be the best. Hopefully, by referencing my code snippets provided below, you can assist me in understanding the issue better.
button.addEventListener("click",login);
function login(){
if(checkInput(username)==false||checkInput(password)==false){
alert("bad input");
return;
}
var params= "Name="+username.value+"&"+"password="+password.value;
var ajax = new XMLHttpRequest();
ajax.responseType = "json";
ajax.addEventListener("load",function(){
console.log(this.response);
messageSpan.innerHTML=this.response[0].message;
if(this.response[0].status==true){
button.style.display="none";
logoutButton.style.display="inline";
createButton.style.display="none";
updateButton.style.display="inline"
}
if(username.value=="admin"){
deleteButton.style.display="inline";
}
});
ajax.open("POST","//cse.taylor.edu/~cos143/sessions.php");
ajax.setRequestHeader("Content-type",
"application/x-www-form-urlencoded");
ajax.send(params);
}
logoutButton.addEventListener("click",logout)
function logout(){
var ajax = new XMLHttpRequest();
ajax.responseType = "json";
ajax.addEventListener("load",function(){
console.log(this.response[0]);
messageSpan.innerHTML=this.response[0].message;
if(this.response[0].status==true){
button.style.display="inline";
logoutButton.style.display="none";
updateButton.style.display="none"
createButton.style.display="inline";
}
deleteButton.style.display="none";
});
ajax.open("DELETE","//cse.taylor.edu/~cos143/sessions.php");
ajax.send();
}
Those are the event listeners for the login and logout buttons, with only ajax.open("~~~
varying. I plan on similar modifications for the delete user button but need guidance!
I tried using 'remove,' but the console indicated an unacceptable request type! Please lend your expertise to resolve this matter!