0

This is my code:

<?php
//check if the form allows user input to be extracted
if(isset($_POST['email']))
    //if so loop begins
{
    //creates a variable called $data that contains the user input for a particular input name from the form
//writes to txt file
$myfile = fopen("form.txt", "w") or die("Unable to open file!");
$email = "Email: ";
fwrite($myfile, $email);
fclose($myfile);
//appends to txt file
$data=$_POST['email'];
    //creates a variable called $fp that contains the function fopen which opens a file called form.txt
$fp = fopen('form.txt', 'a');
    //initiates the function fwrite which displays the user input in the txt file
fwrite($fp, $data); //when echo in html use div center tag
    //closes txt file with the function fclose
fclose($fp);
}
if(isset($_POST['title']))
{
$data=$_POST['title'];
$fp = fopen('form.txt', 'a');
fwrite($fp, $data);
fclose($fp);
}
if(isset($_POST['date']))
{
$data=$_POST['date'];
$fp = fopen('form.txt', 'a');
fwrite($fp, $data);
fclose($fp);
}
if(isset($_POST['link']))
{
$data=$_POST['link'];
$fp = fopen('form.txt', 'a');
fwrite($fp, $data);
fclose($fp);
}
?>      

I want to know how to write the user input to the txt file in php with a space between the header and the input and a linebreak after every one. Whenever I try to use the 'w' more than once the text from the first time it was used is not displayed.

1 Answers1

1

Whenever I try to use the 'w' more than once the text from the first time it was used is not displayed.

Referring to the manual fopen

'w' - write mode will create a new file or reset the pointer to the beginning of the file (overwrite the existing content)

'a' - append mode will set the pointer to the end of the file (add content to the end)

So, that's why you're seeing that behaviour.

With regards to

I want to know how to write the user input to the txt file in php with a space between the header and the input and a linebreak after every one.

I'm unclear from your code and comments what exactly you consider to be the Header, Input and "each one". You code refers to a non-existent loop. So, with the assumption that this actually all just executes in one go and "header is $email and that "input" and "each one" is every instance of $data. Your code should look something more like the following.

(Caveat: I only had a couple of minutes so it could be improved upon with string formatting and such, and I am assuming some php versioning)

Use PHP_EOL for cross-platform end of line. Refer When do I use the PHP constant "PHP_EOL"? for more information.

Again an assumption that this is all one post action and that you want a clean file for each email. If so, from the code you've provided, there appears no need to open the file multiple times.

if( isset( $_POST['email'] ) ) {
    $myfile = fopen("form.txt", "w") or die("Unable to open file!");
    fwrite($myfile, 
        "Email: " . $_POST['email'] . PHP_EOL .
        ( isset( $_POST['title'] ) ? ( $_POST['title'] . PHP_EOL ) : '' ) .
        ( isset( $_POST['date'] ) ? ( $_POST['date'] . PHP_EOL ) : '' ) .
        ( isset( $_POST['link'] ) ? ( $_POST['link'] . PHP_EOL ) : '' ) .
    );
    fclose($myfile);
}

However, if all fields are not captured at the same time, and there is no need for ordering of the entries to be relevant in the file, nor for it to be "clean" then just use append mode instead of write mode.

$myfile = fopen("form.txt", "a") or die("Unable to open file!");
fwrite($myfile, 
    ( isset( $_POST['email'] ) ? ( "Email: " . $_POST['email'] . PHP_EOL ) : '') . 
    ( isset( $_POST['title'] ) ? ( $_POST['title'] . PHP_EOL ) : '' ) .
    ( isset( $_POST['date'] ) ? ( $_POST['date'] . PHP_EOL ) : '' ) .
    ( isset( $_POST['link'] ) ? ($_POST['link'] . PHP_EOL ) : '' ) 
);
fclose($myfile);
Huush
  • 29
  • 5