Код: Выделить всё
public function toggle_checklist_notes(): void
{
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$data = json_decode(file_get_contents('php://input'), true);
$notes = $data['notes'] ?? [];
foreach ($notes as $noteId) {
$note = ChecklistNote::get_by_id($noteId);
if ($note) {
foreach ($note->get_items() as $item) {
$item->toggle_checkbox();
}
}
}
// Get the user ID from the request (or session, as needed)
$user_id = isset($data['user_id']) ? (int)$data['user_id'] : null;
if ($user_id) {
$user = User::get_user_by_id($user_id);
if ($user) {
$updatedNotes = $user->get_checklist_notes();
$responseNotes = array_map(function($note) {
return [
'id' => $note->get_id(),
'title' => $note->get_title(),
'checked_count' => $note->get_checked_count(),
'total_count' => $note->get_total_count(),
];
}, $updatedNotes);
header('Content-Type: application/json');
echo json_encode(['notes' => $responseNotes]);
exit;
}
}
header('Content-Type: application/json');
echo json_encode(['notes' => []]);
exit;
}
}
Код: Выделить всё
document.addEventListener('DOMContentLoaded', function() {
const noteCheckboxes = document.querySelectorAll('.note-checkbox');
const toggleButton = document.getElementById('toggle-check-button');
const selectedUserId = document.querySelector('select\[name="selected_user"\]').value;
noteCheckboxes.forEach(checkbox =\> {
checkbox.addEventListener('change', function() {
toggleButton.disabled = !document.querySelectorAll('.note-checkbox:checked').length;
});
});
toggleButton.addEventListener('click', function() {
const selectedNotes = Array.from(document.querySelectorAll('.note-checkbox:checked')).map(checkbox =\> checkbox.value);
fetch('session1/toggle_checklist_notes', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ notes: selectedNotes, user_id: selectedUserId })
})
.then(response =\> response.json())
.then(response =\> {
updateChecklist(response.notes);
})
.catch(error =\> {
console.error('Error:', error);
});
});
});
function updateChecklist(notes) {
const notesList = document.getElementById('notes-list');
notesList.innerHTML = '';
notes.forEach(note =\> {
const li = document.createElement('li');
li.innerHTML = \`
${note.title} (${note.checked_count}/${note.total_count} checked)\`;
notesList.appendChild(li);
});
}
Подробнее здесь: https://stackoverflow.com/questions/786 ... php-in-url