Hi all,

I am trying to get a simple code to work. It will be an text box for users to post a joke, and then append the file "testfile.txt (see below)

It works just fine, but the problem is, the text submitted is just added to the file (with no line break / carriage return ) The results when viewing the file "testfile.txt" is everything runs together.

I want to add the textbox input, as a new paragraph, in the file testfile.txt

I have tried adding \n and \r everywhere, and the script crashes.

Can anyone help (my short php code below )

      <?php //test.php

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

  $area=$_REQUEST['area'];

  $fd=fopen("testfile.txt","a+");

  fwrite($fd,$area);

  fclose($fd);

  $file_contents=file_get_contents("testfile.txt");


  }

  ?>

  <html>
    <body>
  <center>
Enter your joke below<p>
      <form method="post" action="trythis2.php">
<textarea cols=40 rows=10 name="area"></textarea><br />

  <input type="submit" name="submit" value="submit">
    </form>

  </body>
    </html>

=== Thanks in advance == RickinPV

    1. Welcome to PHPBuilder!

    2. When posting PHP code, please use the board's [noparse]

      ..

      [/noparse] bbcode tags, as they make your code much easier to read and analyze.

    3. RickinPV wrote:

      I have tried adding \n and \r everywhere

      Can you show us how you're trying to add the line breaks? Basically, you need to append (or 'concatenate') a line break character onto the end of the $area variable before writing it out to the file. See this manual page for examples on string concatenation: [man]string[/man].

      I'll use the

       tags in the future  (sorry)
      
      when looking at my code,  in the fwrite statement, is where the textarea inputed text seems to be posted to testfile.txt.  I want to put a carriage return and line feed  AFTER  $area  , so when the master testfile.txt is appended the post includes a linefeed /return at the end, so it doesn't just add text to the beginning of the first line in the testfile.txt.
      
      right now, when the file is appended and then read, it would look like:
      
      joke1postedjoke2postedjoke3posted  (with no breaks)
        RickinPV;10951218 wrote:

        I want to put a carriage return and line feed AFTER $area

        Then use string concatenation and append the string "\r\n" after $area.

        intenz wrote:

        file_put_contents

        Point taken, the file*contents() functions are a more streamlined replacement for fopen/fclose/etc. functions. Still, that function alone won't solve the OP's need for string concatenation to add the line breaks.

          Write a Reply...