Home > OS >  Writing HTML form values to file using PHP
Writing HTML form values to file using PHP

Time:10-08

I'm trying to take the values of a form and use PHP to write them to an existing text file. I've found the below online, but it simply does not work. Does anyone have any ideas? I can't see why it isn't working.

EDIT: By "not working", I mean the text file isn't updated and no "saved" message is shown when "done.php" is loaded. I'm checking the file using "cat data.txt". File permissions are -rwxr--r-- and is owned by root.

   <form action = "done.php" method="POST">
            <textarea name="key" class="key" required="required" minlength="4" placeholder="Key"></textarea>
            <br>
            <div class="field">
                <input type="password" name="password" placeholder="Password">
            </div>
            <br>
            <button type="submit" name="submit" class="btn">Submit</button>
        </form>
<?php

  if(isset($_POST['submit']))
  {
   $key = $_POST['key'];
   $password = $_POST['password'];
   $text = $key . "," . $password . "\n";
   $fp = fopen('data.txt', 'a');

     if(fwrite($fp, $text))  {
         echo 'saved';

     }
 fclose ($fp);    
 ini_set('display_errors', true);
 }
?>

CodePudding user response:

With thanks to CBroe, it turns out to be a file permissions error. Fixed by changing ownership from root. Rookie mistake, sorry.

CodePudding user response:

Try with

$fp = fopen('data.txt', 'a');

Refrence Link

  •  Tags:  
  • php
  • Related