Question: In addition to the PHP code below, I wish to add a Remove Task button after the Add Task button in the Add Task portion
In addition to the PHP code below, I wish to add a Remove Task button after the Add Task button in the Add Task portion of the page. Then, modify the PHP for the page so the Add Task button adds the task to the top of the task list (currently, it adds the task to the bottom of the list), and the Remove Task button removes the task at the top of the list (LIFO).
//get action variable from POST $action = filter_input(INPUT_POST, 'action');
//initialize error messages array $errors = array();
//process switch( $action ) { case 'Add Task': $new_task = filter_input(INPUT_POST, 'newtask'); if (empty($new_task)) { $errors[] = 'The new task cannot be empty.'; } else { array_push($task_list, $new_task); } break; case 'Delete Task': $task_index = filter_input(INPUT_POST, 'taskid', FILTER_VALIDATE_INT); if ($task_index === NULL || $task_index === FALSE) { $errors[] = 'The task cannot be deleted.'; } else { unset($task_list[$task_index]); $task_list = array_values($task_list); } break; // case 'Modify Task': case 'Modify Task': $task_index = filter_input(INPUT_POST, 'taskid', FILTER_VALIDATE_INT); if ($task_index === NULL || $task_index === FALSE) { $errors[] = 'The task cannot be modified.'; } else { $task_to_modify = $task_list[$task_index]; } break; // case 'Save Changes': case 'Save Changes': $i = filter_input(INPUT_POST, 'modifiedtaskid', FILTER_VALIDATE_INT); $modified_task = filter_input(INPUT_POST, 'modifiedtask'); if (empty($modified_task)) { $errors[] = 'The modified task cannot be empty.'; } elseif($i === NULL || $i === FALSE) { $errors[] = 'The task cannot be modified.'; } else { $task_list[$i] = $modified_task; $modified_task = ''; } break; // case 'Cancel Changes': case 'Cancel Changes': $modified_task = ''; break; // case 'Promote Task': case 'Promote Task': $task_index = filter_input(INPUT_POST, 'taskid', FILTER_VALIDATE_INT); if ($task_index === NULL || $task_index === FALSE) { $errors[] = 'The task cannot be promoted.'; } elseif ($task_index == 0) { $errors[] = 'You cannot promote the first task.'; } else { // get the values for the two indexes $task_value = $task_list[$task_index]; $prior_task_value = $task_list[$task_index-1];
// swap the values $task_list[$task_index-1] = $task_value; $task_list[$task_index] = $prior_task_value; break; } // case 'Sort Tasks': case 'Sort Tasks': sort($task_list); break;
}
include('task_list.php'); ?>
Task List Manager
Errors:
Tasks:
There are no tasks in the task list.
- $task ) : ?>
Add Task:
0 && empty($task_to_modify)) : ?>
Select Task:
Task to Modify:
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
