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);
});
}
je n’arrive pas a transmettre en post les values pour le scipt j’ai essayé plusieurs methode mais je n’ai rien dans l’url c’est du php et javascript il faut faire ca dans une seul methode merci de votre aide j’ai pas plus de details je fais ca depuis 1 semaine comment integrer la value dans le header
New contributor
ayman handali is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.