title: How to insert web form data to csv file using php
body: <p>I am trying to achieve data storage in csv file through web form, i tried  below codes which are saved in "form.html", "form1.php", "formTest.csv" and all three are in same directory.  </p>
  
  <pre><code>&lt;form id="form1" name="form1" method="post" action="form1.php"&gt;
  &lt;table class="formatTblClass"&gt;
  &lt;tr&gt;
  
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td width="68"&gt;&lt;span&gt;First Name&lt;/span&gt;&lt;/td&gt;
  &lt;td width="215"&gt;&lt;input class="&lt;?=$aClass;?&gt;" type="text" name="fn" id="fn" /&gt;&lt;/td&gt;
  &lt;td width="62"&gt;&lt;span&gt;Last Name&lt;/span&gt;&lt;/td&gt;
  &lt;td colspan="3"&gt;&lt;input class="&lt;?=$aClass;?&gt;" name="ln" type="text" id="ln" size="50" /&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;div align="center"&gt;
  &lt;input type="submit" name="Submit" id="Submit" value="Submit" /&gt;
  &lt;input type="reset" name="Reset" id="button" value="Reset" /&gt;
  &lt;/div&gt;
  &lt;/table&gt;
  &lt;/form&gt;  
  </code></pre>
  
  <p>Now form1.php script  </p>
  
  <pre><code>&lt;?php
  
  $fn = $_POST['fn'];
  $ln = $_POST['ln'];
  if(empty($fn) || empty($ln))
    {//show the form
    $message = 'Fill in areas in red!';
    $aClass = 'errorClass';
  }
  $cvsData = $fn . "," . $ln . "\n";
  $fp = fopen("formTest.csv","a");
  if($fp){
    fwrite($fp,$cvsData); // Write information to the file
    fclose($fp); // Close the file
  }
  ?&gt;  
  </code></pre>
  
  <p>Now formTest.csv file<br>
  <img src="https://i.stack.imgur.com/XO9yt.png" alt="enter image description here">  </p>
  
  <p>If i run this in browser and again refreshed the csv file i am getting output like below picture  </p>
  
  <p><img src="https://i.stack.imgur.com/PriC0.png" alt="enter image description here">  </p>
  
  <p>How to get proper user inputted output??<br>
  Thanks in advance</p>