Anonymous
Я не могу отправить php по URL [закрыто]
Сообщение
Anonymous » 24 июн 2024, 14:35
Код: Выделить всё
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);
});
}
Я не могу опубликовать значения для скрипта. Я пробовал несколько методов, но в URL-адресе ничего нет. Это php и javascript, вам нужно сделать это одним методом, спасибо за ваша помощь, у меня нет подробностей, я занимаюсь этим уже 1 неделю, как интегрировать значение в заголовок
Подробнее здесь:
https://stackoverflow.com/questions/786 ... php-in-url
1719228938
Anonymous
[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] [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] Я не могу опубликовать значения для скрипта. Я пробовал несколько методов, но в URL-адресе ничего нет. Это php и javascript, вам нужно сделать это одним методом, спасибо за ваша помощь, у меня нет подробностей, я занимаюсь этим уже 1 неделю, как интегрировать значение в заголовок Подробнее здесь: [url]https://stackoverflow.com/questions/78662121/i-cannot-send-php-in-url[/url]