title: PHP write file from input to txt
body: <p>I've searched around this site for an answer but couldnt find any.</p>
  
  <p>I have a form and I'd like to get the contents of the input written into a txt file. To make it simple I just wrote a simple form and a script but it keeps getting me a blank page. Here is what I got</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
      &lt;title&gt;&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;form&gt;
          &lt;form action="myprocessingscript.php" method="post"&gt;
          &lt;input name="field1" type="text" /&gt;
          &lt;input name="field2" type="text" /&gt;
          &lt;input type="submit" name="submit" value="Save Data"&gt;
      &lt;/form&gt;
      &lt;a href='data.txt'&gt;Text file&lt;/a&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p></p>
  
  <p>and here is my PHP file</p>
  
  <pre><code>&lt;?php
  $txt = "data.txt"; 
  $fh = fopen($txt, 'w+'); 
  if (isset($_POST['field1']) &amp;&amp; isset($_POST['field2'])) { // check if both fields are set
     $txt=$_POST['field1'].' - '.$_POST['field2']; 
     file_put_contents('data.txt',$txt."\n",FILE_APPEND); // log to data.txt 
     exit();
  }
      fwrite($fh,$txt); // Write information to the file
      fclose($fh); // Close the file
      ?&gt;
  </code></pre>