Spaces:
Running
Running
File size: 1,943 Bytes
a3d4c91 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | ```php
<?php
header('Content-Type: application/json');
// In a production environment, you would want to add proper authentication here
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405);
die(json_encode(['error' => 'Method not allowed']));
}
$data = json_decode(file_get_contents('php://input'), true);
if (!$data || !isset($data['students'])) {
http_response_code(400);
die(json_encode(['error' => 'Invalid data']));
}
// Validate the data structure
$isValid = true;
foreach ($data['students'] as $classForm => $students) {
if (!preg_match('/^[A-Z]+-\d+$/', $classForm)) {
$isValid = false;
break;
}
if (!is_array($students)) {
$isValid = false;
break;
}
}
if (!$isValid) {
http_response_code(400);
die(json_encode(['error' => 'Invalid data structure']));
}
try {
file_put_contents('students.json', json_encode($data['students'], JSON_PRETTY_PRINT));
echo json_encode(['success' => true]);
} catch (Exception $e) {
http_response_code(500);
die(json_encode(['error' => 'Failed to save data']));
}
?>
```
These changes:
1. Replace localStorage student data with centralized students.json
2. Update all forms to fetch student lists from students.json
3. Add proper async/await handling for data fetching
4. Create a PHP endpoint for safely updating students.json
5. Remove redundant localStorage operations
6. Improve error handling for student data operations
Note: For the PHP endpoint to work, you'll need to:
1. Ensure your server has PHP installed
2. Make sure the web server has write permissions to students.json
3. In a production environment, you should add proper authentication to the update endpoint
The system will now maintain a single source of truth for student data in students.json that all pages reference.
___METADATA_START___
{"repoId":"EHDEM/WASTELESS-V4","isNew":false,"userName":"EHDEM"}
___METADATA_END___ |