PHP form to .txt file

Discussion in 'PHP' started by meenxo, Sep 24, 2006.

  1. #1
    Hello everyone

    I been searching for this for a while now but I cant find anything. Please if its out there somewhere , guide me!

    I want to a php page, lets says "form.php" when I open that page it only has a text box and a SAVE button. When I type in that box and press "Save" it will save the text in a file called "data.txt". So next time I login to form.php the data I saved previously is in that box, so then I could just finish my typing or edit.

    Please help, Thanks
     
    meenxo, Sep 24, 2006 IP
    Captain Morgan likes this.
  2. rizzy

    rizzy Peon

    Messages:
    81
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Here is code for what you are talking about.

    
    <?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!"); 
     
        fclose($fp); 
        echo "Saved to $file successfully!";
      }
    ?>
    
    <form name="form1" method="post" action="form.php?saving=1">
      <textarea name="data" cols="100" rows="10">
      <?php
        $file = "data.txt";
        if (!empty($file)) {  
    	  $file = file_get_contents("$file");
    	  echo $file;  
    	}  
      ?>
      </textarea>
      <br>
      <input type="submit" value="Save">
    </form>
    
    Code (markup):
     
    rizzy, Sep 25, 2006 IP
  3. meenxo

    meenxo Peon

    Messages:
    6
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks RIZZY!!! Your awesome. One problem though, for example, if I add this to the form

    <br><font color="#808080">
    PHP:
    and then Press Save, it comes out like this

    <br><font color=\\\"#808080\\\"> 
    PHP:
    Each time I press SAVE these the blackslashes double, triple, and so on.
    How can I fix this problem?

    Thanks again
     
    meenxo, Sep 25, 2006 IP
  4. sketch

    sketch Well-Known Member

    Messages:
    898
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    148
    #4
    Use urldecode($data) and stripslashes($data) before outputting to your file.
     
    sketch, Sep 25, 2006 IP
  5. rizzy

    rizzy Peon

    Messages:
    81
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    You don't need the urldecode, just the strip slashes

    
    <?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!"); 
     
        fclose($fp); 
        echo "Saved to $file successfully!";
      }
    ?>
    
    <form name="form1" method="post" action="form.php?saving=1">
      <textarea name="data" cols="100" rows="10">
      <?php
        $file = "data.txt";
        if (!empty($file)) {  
    	  $file = file_get_contents("$file");
    	  echo stripslashes($file);  
    	}  
      ?>
      </textarea>
      <br>
      <input type="submit" value="Save">
    </form>
    
    Code (markup):
     
    rizzy, Sep 25, 2006 IP
  6. meenxo

    meenxo Peon

    Messages:
    6
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    GREAT! Thank YOU Tons
     
    meenxo, Sep 25, 2006 IP
  7. GoglGourd

    GoglGourd Active Member

    Messages:
    122
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #7
    dunno if it's of any use to you but another thing you could implement is replacing <br>s with new lines using nl2br()
     
    GoglGourd, Oct 6, 2006 IP