-1

EDIT: I know that I have correctly installed PHP and the localhost server is running properly through MAMP.

I'm trying to create a todo list that saves the input to a txt file using PHP and then displays it. However, I can't get it to work. When I try to add a "todo item," it just disappears.

This first code snippet is where the todo item is added (the index.html file):

<div id="container">
  <h1>To-do List<i class="fa fa-plus" aria-hidden="true"></i></h1>
  <form action="index.php" method="post">
    <input name="addtodo" type="text" placeholder="Add New To-Do">
  </form>
  <ul>
    <li><span><i class="fa fa-trash"></i></span> Go to Potions Class</li>
  </ul>
</div>

This is the index.php file where the todo item should ideally be added to the text file and displayed. It is not doing either.

<?php
        //define variables
        $addtodo = $_POST["addtodo"];
        $DOCUMENT_ROOT = $_SERVER["DOCUMENT_ROOT"];
        $filename = $DOCUMENT_ROOT.'todo.txt';
                        
        //write
        $fp = fopen($filename, 'a');
        $outputLine = $addtodo;
        fwrite($fp, $outputLine);
        fclose($fp);
?>

  <div id="container">
    <h1>To-do List<i class="fa fa-plus" aria-hidden="true"></i></h1>
    <ul>
      <li><span><i class="fa fa-trash"></i></span> Go to Potions Class</li>
    </ul>

    <?php
                    $display = "";
                    //read 
                    $fp = fopen($filename, 'r');
                    
                    while (true) {
                        $line = fgets($fp);
                        
                        if (feof($fp)) {
                            break;
                        }                    
                    $display .= '<li><span><i class="fa fa-trash"></i></span>'.$line.'</li>';
                    }
                    fclose($fp);
                    echo $display;
                ?>
  </div>
  </body>
Nicholas Chan
  • 39
  • 1
  • 4
  • To troubleshoot, I suggest [showing all PHP errors](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display). – showdev Dec 27 '17 at 23:39
  • It should be $DOCUMENT_ROOT.'/'.'todo.txt'. You can use if(file_exists($filename)===false){ exit('file does not exist'); } to see if the file is available, you can also test if it is writable in a similar way is_writable ( $filename ) – iSZ Dec 27 '17 at 23:53

2 Answers2

0

I believe the form submit is being triggered by a JavaScript event, right?

Have you tried printing $filename var to check if it correct? I am not sure if '$_SERVER["DOCUMENT_ROOT"]' always give you a '/' at the end of the path. If it doesn't you should have something like: /your/pathtodo.txt

Also, you can check the todo.txt writing permissions

0

You have a problem with your path, it should be:

$filename = $DOCUMENT_ROOT. DIRECTORY_SEPARATOR .'todo.txt';

DIRECTORY_SEPARATOR is a predefined constant which gets the operating system slash separator (whether it is a forward or backward slash). Read more

Andrew Naguib
  • 3,978
  • 2
  • 21
  • 42