2

I saw this question and I wanted to ask a question in the comment but didn't have enough reputation. So I'm asking the question here.

I have a form in HTML:

<form action="myprocessingscript.php" method="POST">
Name : <input name="field1" type="text" />
Email : <input name="field2" type="text" />
<input type="submit" name="submit" value="Save Data">

And a processing script in php, myprocessingscript.php :

if (isset($_POST['field1']) && isset($_POST['field2'])) {
  $data = 'comments.txt' . $_POST['field1'] . ' ' . $_POST['field2'] . "\n";
  $ret = file_put_contents('comments.txt', $data);

  if ($ret === false) {
    die('There was an error Sending The Comment');
  } 

  else {
    echo "The Comment Has Been Sent Succesfully !";
  }
} 

else {
  die('Fill in The Form Please !');
}

if (isset($_POST['field1']) && isset($_POST['field2'])) {
  $data = 'comments.txt' . $_POST['field1'] . ' ' . $_POST['field2'] . "\n";
  $ret = file_put_contents('comments.txt', $data);

  if ($ret === false) {
    die('There was an error Sending The Comment');
  } 

  else {
    echo "The Comment Has Been Sent Succesfully !";
  }
} 

else {
  die('no post data to process');
}

When I write something in the form to a text file (comments.txt) the previous text is deleted - what should I do?

Community
  • 1
  • 1
Mahdi Abdi
  • 682
  • 6
  • 27
  • What is your question? And, you aren't supposed to ask questions in comments, you are supposed to ask new questions as you have done. – Brad Feb 24 '14 at 15:58
  • 1
    ... the funny thing is - you've not actually asked a question?! – CD001 Feb 24 '14 at 15:59
  • @Brad I Pressed Enter As a Mistake ! I Edited it Afer 20 seconds ! – Mahdi Abdi Feb 24 '14 at 15:59
  • unable to understand question ..its looks funny.. – Pank Feb 24 '14 at 16:00
  • What's with capitalising every word? – superphonic Feb 24 '14 at 16:02
  • 1
    http://stackoverflow.com/a/3536594/362536 – Brad Feb 24 '14 at 16:02
  • 2
    @superphonic Consider for a moment that not everyone on the planet is a native English speaker. – Brad Feb 24 '14 at 16:03
  • @Brad I refuse to believe there is any language out there that arbitrarily capitalises every word within a sentence. Obviously referring to those languages based on the Latin alphabet. – superphonic Feb 24 '14 at 16:07
  • 1
    @superphonic What would that have to do with anything? This person is clearly trying English, and is obviously not familiar with all of the rules. – Brad Feb 24 '14 at 16:08
  • 1
    I've reformatted the question into proper Englanderish like, so you can stop arguing now :P – CD001 Feb 24 '14 at 16:14
  • 1
    Sorry Bros ! if you Downvoted this question because of that i didn't ask my question at first , please remove your vote ! – Mahdi Abdi Feb 24 '14 at 16:19

1 Answers1

5

You just need to add the 'append' flag to file_put_contents() :

file_put_contents('comments.txt', $data, FILE_APPEND);

See : http://uk3.php.net/manual/en/function.file-put-contents.php

CD001
  • 8,332
  • 3
  • 24
  • 28
  • 1
    May wanna add `lock_ex` there, to prevent.. lots of bad stuff :) – Predrag Beocanin Feb 24 '14 at 16:28
  • @SamFisher What Does it Do ?! – Mahdi Abdi Feb 24 '14 at 18:32
  • 1
    LOCK_EX - Exclusive lock (writer). Prevent other processes from accessing the file Prevents changes being done to the file while the changes are beind done to the file :) To add it to the upper stuff, you do: `file_put_contents('comments.txt', $data, FILE_APPEND | LOCK_EX);` – Predrag Beocanin Feb 24 '14 at 20:51