Jempi Posted November 17, 2014 Share Posted November 17, 2014 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> Quote Link to comment Share on other sites More sharing options...
Barand Posted November 17, 2014 Share Posted November 17, 2014 (edited) 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 November 17, 2014 by Barand Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.