Jump to content

To do list


Jempi

Recommended Posts

Hi , i am trying to make a to do list where i can add things i still need to do . i  also want to remove items from the list when they are done , this is what i made now but it still misses a lot , my array keeps getting emptied when i add something. 

im still a beginner in php. This is the file without the css included , i don't think it's relevant to this question to include it.

 

<?php 
 
session_start();
var_dump( $_POST);
 
$items = array();
 
if( ! empty($_POST['item'])) {
        $items[] = $_POST['item'];
    }
 
//<?php for ($i=0; $i < count($items) ; $i++) : 
//<?php endfor;
 
 
 ?>
 
 <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>To do : periodeopdracht</title>
<link href='http://fonts.googleapis.com/css?family=Droid+Sans:400,700' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="css/main.css">
</head>
<body>
        <div class="list">
        <h1 class="header">To do </h1>
             
           <form action="index.php" class="item-add" method="post">
        <ul class="items">
                
                <?php foreach($items as $item): 
        echo '<li>' . $item .  '<li>' ;
        endforeach; ?>
                
        </ul>   
       
        <input type="text" name="item" placeholder="Typ uw nieuw item hier" class="input" autocomplete="off" required>
        <input type="submit" value="toevoegen" class="submit" name="submit">
 
        </form>
        </div>
    </body>
</html>
 
Link to comment
Share on other sites

You need to store in $_SESSION insterad of creating a new array each time one is posted

session_start();
var_dump( $_POST);

if (!isset($_SESSION['items'])) {
    $_SESSION['items'] = array();
}
 
if(isset($_POST['item']) && !empty($_POST['item'])) {
        $_SESSION['items'][] = $_POST['item'];
    }

Use session_start() and this same session array in the form page to list current items

 

Note: unless you then save to a file or db table the items will be lost when the session ends.

Edited by Barand
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.