Files

250 lines
6.9 KiB
JavaScript
Raw Permalink Normal View History

2025-11-25 13:33:49 +05:30
window.onload = function () {
if (document.getElementById('Village_Name')) {
document.getElementById('Village_Name').focus();
}
2026-03-23 11:37:15 +05:30
};
2026-03-24 11:28:14 +05:30
2025-11-25 13:33:49 +05:30
$(document).ready(function () {
// ✅ RUN ONLY IF THIS PAGE HAS FORM/TABLE
if ($('#addForm').length && $('#addTable').length) {
2026-03-24 11:28:14 +05:30
// ✅ DEFAULT VIEW → SHOW FORM
2026-03-24 11:28:14 +05:30
$('#addForm').show();
$('#addTable').hide();
// 🔥 BUTTON TOGGLE
$('#addButton').click(function () {
$('#addForm').show();
$('#addTable').hide();
});
2026-03-24 11:28:14 +05:30
$('#displayButton').click(function () {
$('#addForm').hide();
$('#addTable').show();
});
}
2026-03-24 11:28:14 +05:30
// ===============================
2026-03-23 11:37:15 +05:30
// STATE → DISTRICT
// ===============================
if ($('#state_Id').length) {
2026-03-23 11:37:15 +05:30
$('#state_Id').change(function () {
2026-03-23 11:37:15 +05:30
var stateId = $(this).val();
2026-03-23 11:37:15 +05:30
if (stateId) {
2026-03-23 11:37:15 +05:30
$.ajax({
url: '/get_districts/' + stateId,
type: 'GET',
2026-03-23 11:37:15 +05:30
success: function (data) {
2026-03-23 11:37:15 +05:30
var districtDropdown = $('#district_Id');
2026-03-23 11:37:15 +05:30
districtDropdown.empty();
districtDropdown.append('<option value="" disabled selected>Select District</option>');
2026-03-23 11:37:15 +05:30
data.forEach(function (district) {
districtDropdown.append(
'<option value="' + district.id + '">' + district.name + '</option>'
);
});
2025-11-25 13:33:49 +05:30
districtDropdown.prop('disabled', false);
}
});
}
});
}
2026-03-23 11:37:15 +05:30
// ===============================
2026-03-23 11:37:15 +05:30
// DISTRICT → BLOCK
// ===============================
if ($('#district_Id').length) {
2026-03-23 11:37:15 +05:30
$('#district_Id').change(function () {
2026-03-23 11:37:15 +05:30
var districtId = $(this).val();
2025-11-25 13:33:49 +05:30
if (districtId) {
2026-03-23 11:37:15 +05:30
$.ajax({
url: '/get_blocks/' + districtId,
type: 'GET',
2026-03-23 11:37:15 +05:30
success: function (data) {
2026-03-23 11:37:15 +05:30
var blockDropdown = $('#block_Id');
2026-03-23 11:37:15 +05:30
blockDropdown.empty();
blockDropdown.append('<option value="" disabled selected>Select Block</option>');
2026-03-23 11:37:15 +05:30
data.forEach(function (block) {
blockDropdown.append(
'<option value="' + block.id + '">' + block.name + '</option>'
);
});
2026-03-23 11:37:15 +05:30
blockDropdown.prop('disabled', false);
}
});
}
});
}
2026-03-23 11:37:15 +05:30
// ===============================
// VILLAGE NAME VALIDATION
// ===============================
if ($('#Village_Name').length) {
2026-03-23 11:37:15 +05:30
$('#Village_Name').on('input', function () {
2026-03-23 11:37:15 +05:30
var villageName = $(this).val();
var validPattern = /^[A-Za-z ]*$/;
2026-03-23 11:37:15 +05:30
if (!validPattern.test(villageName)) {
2026-03-23 11:37:15 +05:30
$('#villageMessage')
.text('Only letters and spaces are allowed!')
.css('color', 'red');
2026-03-23 11:37:15 +05:30
$('#submitVillage').prop('disabled', true);
2026-03-23 11:37:15 +05:30
} else {
2026-03-23 11:37:15 +05:30
$('#villageMessage').text('');
$('#submitVillage').prop('disabled', false);
}
});
}
2026-03-23 11:37:15 +05:30
// ===============================
// CHECK DUPLICATE VILLAGE
// ===============================
if ($('#Village_Name').length && $('#block_Id').length) {
2026-03-23 11:37:15 +05:30
$('#Village_Name, #block_Id').on('change keyup', function () {
2026-03-23 11:37:15 +05:30
var blockId = $('#block_Id').val();
var villageName = $('#Village_Name').val().trim();
2026-03-23 11:37:15 +05:30
if (blockId && villageName) {
2026-03-23 11:37:15 +05:30
$.ajax({
url: '/check_village',
type: 'POST',
2026-03-23 11:37:15 +05:30
data: {
block_Id: blockId,
Village_Name: villageName
},
2026-03-23 11:37:15 +05:30
success: function (response) {
2026-03-23 11:37:15 +05:30
if (response.status === 'exists') {
2026-03-23 11:37:15 +05:30
$('#villageMessage')
.text(response.message)
.css('color', 'red');
2026-03-23 11:37:15 +05:30
$('#submitVillage').prop('disabled', true);
2026-03-23 11:37:15 +05:30
} else {
2026-03-23 11:37:15 +05:30
$('#villageMessage')
.text(response.message)
.css('color', 'green');
2026-03-23 11:37:15 +05:30
$('#submitVillage').prop('disabled', false);
}
},
2026-03-23 11:37:15 +05:30
error: function () {
2026-03-23 11:37:15 +05:30
$('#villageMessage')
.text('Error checking village name')
2026-03-23 11:37:15 +05:30
.css('color', 'red');
$('#submitVillage').prop('disabled', true);
2025-11-25 13:33:49 +05:30
}
});
}
});
}
2026-03-23 11:37:15 +05:30
// ===============================
// ADD VILLAGE (SAFE SCOPE FIX)
// ===============================
if ($('#villageForm').length) {
2026-03-23 11:37:15 +05:30
$('#villageForm').submit(function (event) {
2026-03-23 11:37:15 +05:30
event.preventDefault();
2026-03-23 11:37:15 +05:30
$.ajax({
url: '/add_village',
type: 'POST',
data: $(this).serialize(),
2026-03-23 11:37:15 +05:30
success: function (response) {
2026-03-23 11:37:15 +05:30
if (response && response.status === 'success') {
2026-03-23 11:37:15 +05:30
alert(response.message || 'Village added successfully!');
2026-03-23 11:37:15 +05:30
// ✅ clear form
$('#villageForm')[0].reset();
2026-03-23 11:37:15 +05:30
// ✅ switch to table
$('#addForm').hide();
$('#addTable').show();
2026-03-23 11:37:15 +05:30
// optional refresh
location.reload();
2026-03-23 11:37:15 +05:30
} else {
alert(response.message || 'Error adding village. Please try again.');
}
},
2026-03-23 11:37:15 +05:30
error: function () {
alert('An error occurred. Please try again.');
2026-03-23 11:37:15 +05:30
}
});
2026-03-23 11:37:15 +05:30
2025-11-25 13:33:49 +05:30
});
}
2026-03-23 11:37:15 +05:30
2026-03-24 11:28:14 +05:30
});
// ===============================
// DELETE FUNCTION (SAFE)
// ===============================
function deleteVillage(villageId, element) {
if (!confirm("Are you sure you want to delete this village?")) return;
2026-03-24 11:28:14 +05:30
$.ajax({
url: '/delete_village/' + villageId,
type: 'GET',
dataType: 'json',
success: function (response) {
alert(response.message); // ✅ now shows "Village deleted successfully!"
if (element) $(element).closest("tr").remove();
2026-03-24 11:28:14 +05:30
},
error: function () {
alert("Error deleting village. Please try again.");
}
});
}