1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Save PHP form data to a txt file

Discussion in 'PHP' started by Captain Morgan, Mar 17, 2008.

  1. #1
    Hi, I am working with this code I found in this forum awhile back. Now, it works as I want but, it overwrites the previous data in the txt file. I need to keep adding the data to the file without overwriting it.

    Example: http://www.videoboob.com/holly/index.php

    Code I am using:
    <?php
    $saving = $_REQUEST['saving'];
    if ($saving == 1){
    $data = $_POST['data'];
    $file = "data.txt";

    $fp = fopen($file, "w") or die("Couldn't open $file for writing!");
    fwrite($fp, $data) or die("Couldn't write values to file!");
    SEMrush
    fclose($fp);
    echo "Saved to $file successfully!";

    }
    ?>

    <form name="form1" method="post" action="index2.php?saving=1">
    <textarea name="data" cols="100" rows="10">
    Vehicle #:
    RO #:
    Date:
    Tech Assigned:
    Warranty:
    Current Status:
    Comments:
    ---------------------------------------------
    </textarea>
    <br>
    <input type="submit" value="Save">
    </form>
    <p>

    <a href="http://www.videoboob.com/holly/data.txt"><b>VIEW<b></a>

    Can someone help me with this code, please.

    Thanks,
    Joe
     
    Captain Morgan, Mar 17, 2008 IP
    SEMrush
  2. stoli

    stoli Peon

    Messages:
    69
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Change fopen to use append rather than write mode. So change it from:
    fopen($file, "w")

    to:

    fopen($file, "a")
     
    stoli, Mar 17, 2008 IP
    Captain Morgan, Colbyt and NIKOLO like this.
  3. Captain Morgan

    Captain Morgan Peon

    Messages:
    252
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks!!!! Exactly what I was after :)
     
    Captain Morgan, Mar 17, 2008 IP
  4. molex

    molex Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Am working on a project here similar to this one.

    But instead of saving a text field, i would like to save input fields. Right now i can only save the username part but password i can't, can u guys help me on what im doing wrong.

    EDIT: also if there is a way to separate the data with a: , - or /.

    This is the code im using, from the PHP part to the form :

    <?php
    $saving = $_REQUEST['saving'];
    if ($saving == 1){
    $data = $_POST['USERNAME'];
    
    $file = "data.txt";
    
    $fp = fopen($file, "a") or die("Couldn't open $file for login!");
    fwrite($fp, $data) or die("Couldn't open new page!");
    
    fclose($fp);
    echo "Proceed to newly open page.";
    
    }
    ?>
    PHP:
    [ICODE]
    <form onsubmit="window.open ('step2.php')" action="index.php?saving=1" method="post">

    Username:<input name="USERNAME" tabindex="1" maxlength="20" value="" type="text">
    Password:<input name="PASSWORD" tabindex="2" maxlength="20" value="" type="password">

    <input tabindex="3" title="Sign In" onclick="imgClick('IEfix1',this.value)" value="Save" src="home_files/sign-in.gif" alt="[Sign In]" type="image">
    <input name="Reset" class="button2" tabindex="4" title="Reset" onclick="this.form.reset();return false" src="home_files/reset.gif" alt="[Reset]" type="image">
    </form>[/ICODE]
     
    molex, May 15, 2009 IP
  5. mrmaf

    mrmaf Peon

    Messages:
    41
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    mrmaf, May 16, 2009 IP
  6. deimac

    deimac Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    First of all, thank you for somehow keeping a 7 year old thread visible - provides a solution I have been looking for for several days straight (eyeballs burning)..Applied and changed the code and it works greaat. What I bump into is that, rather than 1 textarea, I am trying to save a whole set of fields from a form like this. Can I? If so, how can I put fiels seperators in the output saved to the text file?Any help is welcome!Maurice
     
    deimac, Dec 13, 2011 IP
  7. deimac

    deimac Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    found the answer to the mutliple field save in a related thread - but not the solution for the field seperator issue
     
    deimac, Dec 13, 2011 IP
  8. bertbjan

    bertbjan Greenhorn

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    21
    #8
    hi i found this to google an i never connect to data base, the database called notes.txt, any who can help, please,

    <html>
    <head><title> My guestbook</title>
    <body>
    <h1>Welcome to my Guestbook</h1>
    <h2>Please write me a little note below</h2>
    <form action="" method="POST">
    <input type="text" name="user" placeholder="Name" />
    <br />
    <textarea cols="40" rows="5" name="note" placeholder="Comments" wrap="virtual"></textarea>
    <br />
    <input type="submit" name="submit" value="submit" />
    </form>
    <?php

    if (isset($_POST['submit'])){
    $user = $_POST['user'];
    $note = $_POST['note'];
    if(!empty($user) && !empty($note)) {
    $msg = $user . ' : ' . $note;
    //will open a file
    $fp = fopen("notes.txt", "r") or die ("can't open file");
    //will write to file
    fwrite($fp, $msg."\n");
    fclose($fp);
    }
    }
    ?>
    <h2>The entries so far:</h2>
    <?php
    $file = fopen("notes.txt", "r") or exit("unable to open file!");
    //output a line of the file until the end is reached
    while(!feof($file))
    {
    //will return each line with a break
    echo fgets($file). '<br />';
    }
    fclose($file);
    ?>
    </body>
    </html>
     
    bertbjan, Jun 24, 2014 IP
  9. Again3

    Again3 Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #9
    I only saw this post now.
    But if you wanna write into file, only need to change r to w (write) or w+ (read/write)
    $fp = fopen("notes.txt", "r") or die ("can't open file");
    PHP:
    to
    $fp = fopen("notes.txt", "w") or die ("can't open file");
    PHP:
    Maybe this can be usefull for someone.
     
    Again3, Aug 21, 2016 IP
  10. pagecoder

    pagecoder Greenhorn

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    21
    #10
    y
    you can add any seperator like '<br/>' or any other character to seperate the fields.
    You append instead of writing to file if you are not trying to overwrite the file's content.
     
    pagecoder, Oct 5, 2016 IP