0

Please ignore the "read" select option. I am still learning write and append for now. and I wanted to come up with a script that does those two things for me for now before I add more functionality to it. But the error message, that has been displayed below(check the "Error message" picture please) just won't go away.

<?php

    $file_handler = fopen('list_of_names.txt', $choice);

    if(isset($_POST['action'])){

        $choice = $_POST['action'];
        echo $choice;

    }

    if(isset($_POST['name'])){
        $enterd_name = $_POST['name'];
        if(!empty($_POST['name'])){
            fwrite($file_handler, $enterd_name."\n");
        }
    }

    fclose($file_handler);

?>

<form action="file_handling.php" method="POST">

    <label for="name">Name: </label>
    <input type="text" name="name"><br><br>

    <select name="action">
        <option value="a">Append</option>
        <option value="w">re-write</option>
        <option value="r">read</option>
    </select><br><br>

    <input type="submit" value="Submit">

</form>

Error messages

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
  • 1
    Move $file_handler = fopen... below isset. You are using choice before setting it – danielson317 Apr 08 '20 at 21:02
  • The fopen is probably failing due to file permissions. – danielson317 Apr 08 '20 at 21:04
  • @danielson317 Still the same mate. – Kevin Chandarana Apr 08 '20 at 21:07
  • Place all your php code inside this if statement: if (isset($_SERVER['REQUEST_METHOD']) && ($_SERVER['REQUEST_METHOD'] == 'POST')) { // Submit code here. } – danielson317 Apr 08 '20 at 22:21
  • `$choice = $_POST['action']; $file_handler = fopen('list_of_names.txt', $choice); if (!$file_handler) { die('File does not exist.'); } fwrite() fclose()` – danielson317 Apr 08 '20 at 22:22
  • [Please don't add "solved" to your title or question](https://meta.stackexchange.com/a/116105/248627). Instead, [mark an answer correct by clicking on the checkmark](https://meta.stackexchange.com/q/5234/248627). You can also [add your own answer](https://stackoverflow.com/help/self-answer) and accept it if none of the ones you received solved your problem. Also, please don't SHOUT. If you need to emphasize something you can [do it with Markdown](https://stackoverflow.com/help/privileges/comment). – ChrisGPT was on strike May 01 '20 at 14:12

0 Answers0