2025-12-06 23:17:22 +05:30
|
|
|
document.addEventListener("DOMContentLoaded", function () {
|
|
|
|
|
const yearDropdown = document.getElementById("year");
|
2025-12-08 13:40:33 +05:30
|
|
|
const errorDiv = document.getElementById("yearError");
|
|
|
|
|
|
|
|
|
|
// Get the form dynamically
|
|
|
|
|
const form = document.querySelector("form"); // get from id as table name
|
|
|
|
|
|
|
|
|
|
// Dynamic table name = form id
|
|
|
|
|
const tableName = form.id.toLowerCase();
|
2025-12-06 23:17:22 +05:30
|
|
|
|
|
|
|
|
const currentYear = new Date().getFullYear();
|
|
|
|
|
const startYear = 1990;
|
|
|
|
|
|
2025-12-08 13:40:33 +05:30
|
|
|
// Fill Year dropdown
|
2025-12-06 23:17:22 +05:30
|
|
|
for (let y = currentYear; y >= startYear; y--) {
|
|
|
|
|
let nextYear = y + 1;
|
|
|
|
|
|
|
|
|
|
let option = document.createElement("option");
|
|
|
|
|
option.value = y;
|
|
|
|
|
option.textContent = `AY ${y}-${nextYear}`;
|
|
|
|
|
|
|
|
|
|
yearDropdown.appendChild(option);
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-08 13:40:33 +05:30
|
|
|
// Validate selected year
|
|
|
|
|
yearDropdown.addEventListener("change", function () {
|
|
|
|
|
let selectedYear = parseInt(this.value);
|
|
|
|
|
|
|
|
|
|
fetch("/check_year", {
|
|
|
|
|
method: "POST",
|
|
|
|
|
headers: { "Content-Type": "application/json" },
|
|
|
|
|
body: JSON.stringify({
|
|
|
|
|
table: tableName, // dynamic table name
|
|
|
|
|
year: selectedYear
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
.then(res => res.json())
|
|
|
|
|
.then(data => {
|
|
|
|
|
if (data.exists) {
|
|
|
|
|
errorDiv.style.display = "block";
|
|
|
|
|
errorDiv.innerText = `Year ${selectedYear} already exists!`;
|
|
|
|
|
|
|
|
|
|
// Block submission
|
|
|
|
|
form.onsubmit = function () { return false; };
|
|
|
|
|
} else {
|
|
|
|
|
errorDiv.style.display = "none";
|
|
|
|
|
form.onsubmit = null; // Allow submit
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|