Search code examples
phphtmlformspostfwrite

Can´t save $_POST from <form> into .html


I try to save $_POST from a <form> into a .txt file. If i click on the submit button, my code will save only the $date variable, and not $date & $hour.

This is my form in "form.php"

<form method="post" action="save.php" id="formID">
 <input type="text" name="Name" placeholder="Name" id="nameFieldID">
  <input type="text" name="Hour" placeholder="Hour" id="hourFieldID">
   <input type="submit" name="" value="" id="submitButton">
    </form>

This is the code in my "save.php"

<?php
setlocale(LC_ALL, "de_DE.utf8");
date_default_timezone_set('Europe/Berlin');
error_reporting(E_ALL);

$date = date("d.M Y");
$time = date("H:i:s");
$name = $_POST["Name"];
$hour = $_POST["Hour"];

$file = "nameAndHour.txt";
$handle = fopen($file, "a");
$details = fread($handle, filesize($file));

fclose($handle);

$handle = fopen("nameAndHour.txt", "a");
fwrite($handle, $detail);
fwrite($handle, $date, "\t");
fwrite($handle, $time, "\t");
fwrite($handle, $name, "\t");
fwrite($handle, $hour);
fclose($handle);

include('form.php');
?>

Could anybody help me, to get the $_POST variables in the my .txt file? What am I doing wrong?


Solution

  • Firstly, make sure the (text) file exists before attempting to write to it and has proper permissions to write to, otherwise you will receive the following warning:

    Warning: fread(): Length parameter must be greater than 0

    • Sidenote: Consult my footnotes.

    You then need to replace all commas for , "\t" with dots . "\t".

    That is why you're getting the following warning(s):

    Warning: fwrite() expects parameter 3 to be long, string given

    Rewrite:

    <?php 
    setlocale(LC_ALL, "de_DE.utf8");
    date_default_timezone_set('Europe/Berlin');
    error_reporting(E_ALL);
    
    $date = date("d.M Y");
    $time = date("H:i:s");
    $name = $_POST["Name"];
    $hour = $_POST["Hour"];
    
    $file = "nameAndHour.txt";
    $handle = fopen($file, "a");
    $detail = fread($handle, filesize($file));
    
    fclose($handle);
    
    $handle = fopen("nameAndHour.txt", "a");
    fwrite($handle, $detail);
    fwrite($handle, $date . "\t");
    fwrite($handle, $time . "\t");
    fwrite($handle, $name . "\t");
    fwrite($handle, $hour);
    
    fclose($handle);
    
    include('form.php');
    ?>
    

    Footnotes:

    You could also check to see if the file exists, using file_exists() with a conditional statement.

    Example:

    if (file_exists("nameAndHour.txt")) {
    // execute code
    ...
    }
    
    else{
       echo "File does not exist.";
    }
    

    Another thing. About your last line:

    fwrite($handle, $hour);
    

    You should add a "\n" for it so that there is a carriage return inserted after the last entry, otherwise your file will be written in one long line.

    fwrite($handle, $hour . "\n");
    

    "Is there also a way to put the details in a table? Can I use HTML?"

    Edit: (a quick HTML table example)

    To write data in an HTML table, you can use something like this:

    $table1 = "<table>";
    $table2 = "</table>";
    
    $tr1 = "<tr>";
    $tr2 = "</tr>";
    
    $td1 = "<td>";
    $td2 = "</td>";
    
    fwrite($handle, $table1 . "\n");
    
    fwrite($handle, $tr1 . "\n");
    fwrite($handle, $td1 . "\n");
    
    fwrite($handle, $date . "\t");
    fwrite($handle, $time . "\t");
    fwrite($handle, $name . "\t");
    fwrite($handle, $hour . "\n");
    
    fwrite($handle, $tr2 . "\n");
    fwrite($handle, $td2 . "\n");
    
    fwrite($handle, $table2 . "\n");
    

    will write out, and as an example:

    <table>
    <tr>
    <td>
    28.May 2015 21:41:35    data1a  data1b
    </tr>
    </td>
    </table>
    
    • But you will need to experiment with that. I cannot elaborate anymore on this, since it was not part of the original question.