Results 1 to 4 of 4

Thread: php form to text append

  1. #1
    Join Date
    Apr 2009
    Location
    South Carolina
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default php form to text append

    I am new to this forum and new to PHP. But, I'm looking for help with a piece of code I have been working on. What I'm trying to do is use a form to post to a textfile and each time someone uses the form, have the text be appended. I started out with a simple form design to see if I could get it to work and then I plan on doing something more complex with it, but I can't seem to get the simple one to work right.
    My form code:
    HTML Code:
    <form action="addition.php" method="post">
    
    <input type="text" name="addition"> <br>
    <input type="submit" value="submit">
    </form>
    My php (addition.php):

    Code:
    $fn = "file.txt";
    $file = fopen($fn, "a+");
    $size = filesize($fn);
    $space.= "\n";
    if($_POST['addition']) fwrite($file, $_POST['addition']);
    
    $text = fread($file, $size);
    fwrite ($file, $space);
    fwrite ($file, "____");
    fclose($file);

    My problem is that I can't figure out how to have the next post, input a carriage return so that the new data starts on a new line, or even to input some text before the user's input. Any help would be appreciated.
    Last edited by Snookerman; 04-27-2009 at 12:57 PM. Reason: removed unnecessary styling and added “Resolved” prefix

  2. #2
    Join Date
    Mar 2009
    Location
    Chennai, India
    Posts
    77
    Thanks
    16
    Thanked 7 Times in 6 Posts

    Default

    Replace the line
    Code:
    $space.= "\n";
    with
    Code:
    $space.= chr(13) . chr(10);
    It will work.. I tested with your code.

  3. #3
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    Alternatively you can write the space like this:

    PHP Code:
    $space "\r\n"
    And then I've added some validation, this also shows the output of the text file after the fwrite:

    PHP Code:
    if($_POST['addition']) {
        
    $fn "file.txt";
        
    $file fopen($fn"a+");
        
    $space "<br />\r\n";
        
    fwrite($file$_POST['addition']);
        
    fwrite ($file$space);
        
    fclose($file);
        
    // Open file again to read new content
        
    $reader fopen($fn"r");
        
    $size filesize($fn);
        
    $data fread($reader$size);
        echo 
    "Your message has been added.<br />\n";
        echo 
    $data;
    }
    else
        echo 
    'You didn\'t enter any text.'

  4. #4
    Join Date
    Apr 2009
    Location
    South Carolina
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Thanks guys. I appreciate it.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •