OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

i cannot send php in url [closed]

  • Thread starter Thread starter ayman handali
  • Start date Start date
A

ayman handali

Guest
Code:
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;
    }
}


Code:
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

<pre><code>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;
}
}
</code></pre>
<hr />
<pre><code>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);
});

}
</code></pre>
<p>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</p>
 

Latest posts

Top