From 336257ecaf755447cf8d0c2ad2e4a9e4818ca240 Mon Sep 17 00:00:00 2001 From: bochard Date: Wed, 11 Jun 2025 17:55:01 +0800 Subject: [PATCH] edit task feature done --- Classes/Tasks.php | 3 ++- TODO.TXT | 15 +++++++-------- includes/edit_task.php | 29 ++++++++++++++++++++--------- js/tasks.js | 18 +++++++++++------- 4 files changed, 40 insertions(+), 25 deletions(-) diff --git a/Classes/Tasks.php b/Classes/Tasks.php index 236d7bf..0089e3a 100644 --- a/Classes/Tasks.php +++ b/Classes/Tasks.php @@ -35,9 +35,10 @@ class Tasks extends DbConn { } public function editTask($pdo, $taskId, $newInput){ - $query = 'UPDATE tasks SET task = WHERE task_id = ;taskId;'; + $query = 'UPDATE tasks SET task = :newInput WHERE task_id = :taskId;'; $stmt = $pdo->prepare($query); $stmt->bindParam(':taskId', $taskId); + $stmt->bindParam(':newInput', $newInput); $stmt->execute(); } diff --git a/TODO.TXT b/TODO.TXT index d8e941a..76f2474 100644 --- a/TODO.TXT +++ b/TODO.TXT @@ -1,8 +1,7 @@ -1. encrypt passwords on signup -2. add a session timer when user logged in -3. don't give direct access to paths esp. when usr. is not logged in or that path must not be accessible -4. add a short animation loading progress bar when user did some actions to a task -6. create 'editing task' feature -7. create 'task sorting' feature -8. create 'task search' feature -9. when popup is open, the main window will be unaccessible \ No newline at end of file +-encrypt passwords on signup +-add a session timer when user logged in +-don't give direct access to paths esp. when usr. is not logged in or that path must not be accessible +-add a short animation loading progress bar when user did some actions to a task +-create 'task sorting' feature +-create 'task search' feature +-when popup is open, the main window will be unaccessible \ No newline at end of file diff --git a/includes/edit_task.php b/includes/edit_task.php index 0c8001e..48e8e5f 100644 --- a/includes/edit_task.php +++ b/includes/edit_task.php @@ -10,8 +10,10 @@ require_once __DIR__ . '/../Classes/Tasks.php'; header('Content-Type: application/json'); if($_SERVER['REQUEST_METHOD'] === 'POST'){ - $taskIds = json_decode(file_get_contents('php://input'), true); - $newInput = null; + $input = json_decode(file_get_contents('php://input'), true); + + $taskId = $input['taskId'] ?? null; + $newInput = $input['newInput'] ?? null; try{ $dbconn = new DbConn(); @@ -19,15 +21,24 @@ if($_SERVER['REQUEST_METHOD'] === 'POST'){ $userId = $_SESSION['user_data']['user_id']; $tasks = new Tasks($pdo, $userId); - $error = $tasks->error; - - foreach($taskIds as $taskId){ - $tasks->isInputEmpty($taskId); - if(empty($error)){ - $tasks->editTask($pdo, $taskId, $newInput); - } + + $tasks->isInputEmpty($newInput); + + if(empty($tasks->error)){ + $tasks->editTask($pdo, $taskId, $newInput); + $response = [ + 'status' => 'success', + 'message' => 'Task deleted successfully!', + ]; + } else { + $response = [ + 'status' => 'failed', + 'message' => 'Failed to delete task.', + ]; } + echo json_encode($response); + } catch(PDOException $e){ die("Query failed: {$e}"); } diff --git a/js/tasks.js b/js/tasks.js index 893fa8d..927d2d3 100644 --- a/js/tasks.js +++ b/js/tasks.js @@ -121,20 +121,23 @@ function addTask(){ //===== EDIT TASK =====// function editTask(){ - const taskInput = document.getElementById('edit-input-task-name'); - const formData = new FormData(editTaskForm); - const taskName = formData.get('edit-input-task-name').trim(); + const taskName = document.getElementById('edit-input-task-name').value.trim(); // replace placeholder text when no input if(!taskName){ - taskInput.placeholder = 'Please fill this input...'; + document.getElementById('edit-input-task-name').placeholder = 'Please fill this input...'; return; } + const payload = { + taskId: isCheckedArray[0], + newInput: taskName + }; + fetch('./includes/edit_task.php', { method: 'POST', headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify(isCheckedArray) + body: JSON.stringify(payload) }) .then(function(response){ console.log(response); @@ -145,7 +148,8 @@ function editTask(){ setTimeout(function(){ loadTasks(); }, 500); - // setDisabledAttributesOnEditBtn(); + setDisabledAttributesOnEditBtn(); + popupEditTaskClose(); }) .catch(function(error){ console.error(error); @@ -187,7 +191,7 @@ function resetTaskInputPlaceholderForAddBtn(){ function resetTaskInputPlaceholderForEditBtn(){ const taskInput = document.getElementById('edit-input-task-name'); setTimeout(function(){ - taskInput.placeholder = 'New task...'; + taskInput.placeholder = 'Edit task...'; }, 200); } -- 2.39.5