sermon-notes/public/theme/assets/js/register.js

56 lines
1.7 KiB
JavaScript
Raw Normal View History

2024-05-13 21:09:41 -04:00
// Get references to the form elements
const nameInput = document.getElementById("name");
const emailInput = document.getElementById("emailAddress");
const passwordInput = document.getElementById("password");
const confirmPasswordInput = document.getElementById("confirmPassword");
const csrfToken = document.getElementById("csrfToken").value;
// Add event listeners to the form
const registerBtn = document.querySelector("#register-btn");
registerBtn.addEventListener("click", handleSubmit);
// Function to handle form submission
function handleSubmit(event) {
// Prevent default form submission behavior
event.preventDefault();
// Validate input
const name = nameInput.value;
const email = emailInput.value;
const password = passwordInput.value;
const confirmPassword = confirmPasswordInput.value;
if (name === "" || email === "" || password === "") {
alert("Please fill in all fields.");
return;
}
if (password !== confirmPassword) {
alert("Passwords do not match.");
return;
}
// Send data to server for processing
const data = {
"name": name,
"email": email,
"password": password,
"plainPassword": password,
"csrf_token": csrfToken
};
fetch("/index.php/register", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(data),
})
.then((response) => response.json())
.then((data) => {
console.log("User registration successful.");
})
.catch((error) => {
console.error("Error registering user:", error);
});
}