function showCustomAlert(message, type) { const alertBox = document.getElementById("customAlert"); const alertIcon = document.getElementById("alertIcon"); const alertMessage = document.getElementById("alertMessage"); alertMessage.textContent = message; if (type === "success") { alertBox.className = "custom-alert success show"; alertIcon.innerHTML = "?"; // Green checkmark } else { alertBox.className = "custom-alert error show"; alertIcon.innerHTML = "?"; // Red X } // Hide alert after 3 seconds setTimeout(() => { alertBox.className = "custom-alert"; }, 3000); } const salesF = document.querySelector('.sidebar-form form'), salesButton = salesF.querySelector('.salesBtn'); // Disable form's default submission salesF.onsubmit = (e) => { e.preventDefault(); }; // Handle the signup button click salesButton.onclick = () => { salesButton.textContent = "processing..."; salesButton.disabled = true; // Disable the button during execution // Fetch the CSRF token dynamically fetch('./csrf/csrf_token.php') .then(response => response.text()) .then(csrfToken => { document.cookie = `csrf_token=${csrfToken}; path=/; Secure; HttpOnly;`; // Add CSRF token to the form data const formData = new FormData(salesF); formData.append('csrf_token', csrfToken); // AJAX request to handle the registration let xhr = new XMLHttpRequest(); xhr.open("POST", "./code/sales_request.php", true); xhr.onload = () => { if (xhr.readyState === XMLHttpRequest.DONE) { if (xhr.status == 200) { let data = xhr.response; if (data == "success") { salesButton.textContent = "Shop Now"; salesButton.disabled = false; showCustomAlert("We got your reservation. We'll call you to confirm stock & delivery.", "success"); // var timer = setTimeout(function () { // // $("#cart-counter").load(window.location.href + " #cart-counter"); // // $("#withdrawCardRefresh").load(window.location.href + " #withdrawCardRefresh"); // location.reload(); // }, 1500) } else { // Handle error feedback salesButton.textContent = "Shop Now"; // Change button text back to "Signup" salesButton.disabled = false; // Enable the button showCustomAlert(data, "error"); } } } }; // Send form data with the CSRF token xhr.send(formData); }) .catch(error => { // Handle CSRF token fetch errors console.error("Error fetching CSRF token:", error); salesButton.textContent = "Subscribe"; // Re-enable the button salesButton.disabled = false; signuperror.textContent = "An error occurred. Please try again."; signuperror.style.display = "block"; }); };