Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7608985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:03:43+00:00 2026-05-31T01:03:43+00:00

I am working on a task list manager for my class that I am

  • 0

I am working on a task list manager for my class that I am in. I have to include sessions and I need to push each task into an array and be able to delete it from the array.

I think I am doing it correctly,but when I run the program, the task list shows up as empty. I think I may not be adding the tasks to the task-list array correctly, and I really would like a fresh set of eyes to take a look, because maybe you will see what I haven’t.

index.php

<?php
if (isset($_POST['tasklist'])) {
$task_list = $_POST['tasklist'];
} else {
$task_list = array();
}

// put the array in a session variable
$_SESSION['tasks']=$task_list;
$_SESSION['errors']=$errors;

//start the session to last one year
$lifetime = 60 * 60 * 24 * 365;     // 1 year in seconds
ini_set('session.gc_maxlifetime', $lifetime);
//session_set_cookie_params($lifetime, '/');
session_start();

$errors = array();

switch( $_POST['action'] ) {
    case 'add':
    $new_task = $_POST['newtask'];
    if (empty($new_task)) {
        $_SESSION['errors'] = 'The new task cannot be empty.';
    } else {
        $_SESSION['tasks'] = $new_task;
    }
    break;
case 'delete':
    $task_index = $_POST['taskid'];
    unset($task_list[$task_index]);
    $_SESSION['tasks'] = array_values($_SESSION['tasks']);
    break;
}



include('task_list.php');
?>

task_list.php

   <?php
   session_start(); // Access the current session.

?>
</head>
<body>
<div id="page">
<div id="header">
    <h1>Task List Manager</h1>
</div>
<div id="main">

<!-- part 1: the errors -->
<?php if (count($_SESSION['errors']) > 0) : ?>
<h2>Errors</h2>
<ul>
    <?php foreach($_SESSION['errors'] as $error) : ?>
        <li><?php echo $error; ?></li>
    <?php endforeach; ?>
</ul>
<?php endif; ?>

<!-- part 2: the tasks -->
<h2>Tasks</h2>
<?php if (count($_SESSION['tasks']) == 0) : ?>
    <p>There are no tasks in the task list.</p>
<?php else: ?>
    <ul>
    <?php foreach($_SESSION['tasks'] as $id => $task) : ?>
        <li><?php echo $id + 1 . '. ' . $task; ?></li>
    <?php endforeach; ?>
    </ul>
<?php endif; ?>
<br />

<!-- part 3: the add form -->
<h2>Add Task</h2>
<form action="." method="post" >
    <?php foreach($_SESSION['tasks'] as $task) :
      array_push($_SESSION['tasks'],$task);
     endforeach; ?>
    <!--<input type="hidden" name="action" value="add"/> -->
    <label>Task:</label>
    <input type="text" name="<?php $_SESSION['tasks'] ?>" id="newtask" value="<?php     echo $task; ?>" /> <br />
    <label>&nbsp;</label>
    <input type="submit" value="Add Task"/>
</form>
<br />


<!-- part 4: the delete form -->
<?php if (count($_SESSION['tasks']) > 0) : ?>
<h2>Delete Task</h2>
<form action="." method="post" >
    <?php foreach($_SESSION['tasks'] as $task) :
      $_SESSION['tasks']=array_diff($_SESSION['tasks'],$task);
     endforeach; ?>
    <!--<input type="hidden" name="action" value="delete"/> -->
    <label>Task:</label>
    <select name="taskid">
        <?php foreach($_SESSION['tasks'] as $id => $task) : ?>
            <option value="<?php echo $id; ?>">
                <?php echo $task; ?>
            </option>
        <?php endforeach; ?>
    </select>
    <br />
    <label>&nbsp;</label>
    <input type="submit" value="Delete Task"/>
</form>
<?php endif; ?>

</div><!-- end main -->
</div><!-- end page -->
</body>
</html>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T01:03:45+00:00Added an answer on May 31, 2026 at 1:03 am

    You need to call session_start() before any interaction with the session. So if the files are accessed separately then you need it in both. If task_list.php is only included in index.php then just move it to the top of index.php. Additionally, in your logic for the add action you dont have your syntax right to add a task to $_SESSION['tasks'] array, instead you are overwriting it with whatever is in $new_task.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have task schedule for backup directory list of wwwroot. For that I have
I'm working on an ASP.NET MVC task list and I'd like to get fancy
I'm working on a task system and I'd like to be able to submit
Assume that you're working a x86 32-bits system. Your task is to implement the
Still working my way through this program. Next task on my to-do list is
I'm working with a hand in task that is like a simple bank with
I am working with a custom list I've built in SharePoint. I have a
I have a list of tasks that I allow a user to sort, currently
I'm currently working on implementing a list-type structure at work, and I need it
So - working with C# and Windows Forms, I have a bunch of Task

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.