I am having trouble getting some values stored as an array within a session.
The code for the index file is:
<?php if (isset($_POST['tasklist'])) { $task_list = $_POST['tasklist']; } else { $task_list = array(); } //start the session to last one year $lifetime = 60 * 60 * 24 * 365; // 1 year in seconds session_set_cookie_params($lifetime, '/'); session_start(); // Create a item list array if needed if (empty($_SESSION['itemlist'])) $_SESSION['itemlist'] = array(); $errors = array(); switch( $_POST['action'] ) { case 'add': $new_task = $_POST['newtask']; if (empty($new_task)) { $errors[] = 'The new task cannot be empty.'; } else { $task_list[] = $new_task; } break; case 'delete': $task_index = $_POST['taskid']; unset($task_list[$task_index]); $task_list = array_values($task_list); break; } include('task_list.php'); ?>And the code for the form is:
<?php if (count($errors) > 0) : ?><h2>Errors</h2>
<ul>
<?php foreach($errors as $error) : ?>
<li><?php echo $error; ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<!-- part 2: the tasks -->
<h2>Tasks</h2>
<?php if (count($task_list) == 0) : ?>
<p>There are no tasks in the task list.</p>
<?php else: ?>
<ul>
<?php foreach($task_list 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" >
<input type="hidden" name="action" value="add"/>
<label>Task:</label>
<input type="text" name="newtask" id="newtask" /> <br />
<label> </label>
<input type="submit" value="Add Task"/>
</form>
<br />
<!-- part 4: the delete form -->
<?php if (count($task_list) > 0) : ?>
<h2>Delete Task</h2>
<form action="." method="post" >
<input type="hidden" name="action" value="delete"/>
<label>Task:</label>
<select name="taskid">
<?php foreach($task_list as $id => $task) : ?>
<option value="<?php echo $id; ?>">
<?php echo $task; ?>
</option>
<?php endforeach; ?>
</select>
<br />
<label> </label>
<input type="submit" value="Delete Task"/>
</form>
<?php endif; ?>
Session ID: <?php echo session_id(); ?>
The point of the page is to allow the user to add tasks, which show up in an ordered list. It also allows the user to delete specific tasks. Any help?