web1/js/login.js
2025-12-04 21:01:53 +09:00

44 lines
1.4 KiB
JavaScript

document.addEventListener("DOMContentLoaded", () => {
const btn_login = document.querySelector("#btn_login")
btn_login.addEventListener("click", () => {
const f_id = document.querySelector("#f_id")
if(f_id.value == '') {
alert('아이디를 입력하세요')
f_id.focus()
return false
}
const f_pw = document.querySelector("#f_pw")
if(f_pw.value == '') {
alert('비밀번호를 입력하세요')
f_pw.focus()
return false
}
// AJAX
const xhr = new XMLHttpRequest()
xhr.open("POST", "./pg/login_process.php", true)
const f1 = new FormData()
f1.append("id", f_id.value)
f1.append("pw", f_pw.value)
xhr.send(f1)
xhr.onload = () => {
if (xhr.status == 200) {
const data = JSON.parse(xhr.responseText);
console.log(data);
if (data.result === 'login_success') {
alert('로그인 성공!');
location.href = "/member/index.php"; // 이동
} else if (data.result === 'login_fail') {
alert('아이디 또는 비밀번호가 일치하지 않습니다.');
}
} else {
alert('로그인에 실패하였습니다. 다시 시도해 주세요');
}
}
})
})