$(document).ready(function() { // Fetch Students based on selected SchoolID and class $('#class, #SchoolID').on('change', function() { var schoolID = $('#SchoolID').val(); var classID = $('#class').val(); if (schoolID && classID) { $.post('fetch_students.php', { action: 'fetch_students_with_null_tests', class: classID, SchoolID: schoolID }, function(data) { console.log('Raw response:', data); try { var parsedData = typeof data === 'string' ? JSON.parse(data.trim()) : data; var students = parsedData.students || []; var rows = ''; if (students.length === 0) { rows = 'لا يوجد طلاب'; } else { students.forEach(function(student) { rows += ''; rows += '' + student.Name + ''; for (var i = 1; i <= 13; i++) { let maxValue = [5,5,5,5,5,5,3,2,5,2,3,5,10][i-1]; rows += ` `; } rows += ''; }); } $('#student-table-body').html(rows); } catch (e) { console.error('Error parsing response:', e, data); alert("خطأ في تحميل البيانات. يرجى المحاولة مرة أخرى."); } }).fail(function(xhr, status, error) { console.error('AJAX Error:', status, error); alert("خطأ في الاتصال بالخادم. يرجى المحاولة مرة أخرى."); }); } else { $('#student-table-body').html(''); } }); function validateInput(input, max) { const value = parseInt(input.value); if (isNaN(value) || value < 0 || value > max) { alert(`القيمة يجب أن تكون بين 0 و ${max}`); input.value = ''; } } });