title: Write to file upon button press
body: <p>Trying to write to a txt file upon button click. Not sure how to do it and my code isnt working. Im starting to think i might need AJAX? If so, can someone give me a quick pointer in the right direction? Please and thankyou in advance! </p>
  
  <p>Here's my code:</p>
  
  <pre><code>&lt;?php
   //write to file on button event
  
  function buttonWrite()
  {
  $filename = 'button.txt';
  @ $fp = fopen("messages/".$filename, 'wb');
  if (!$fp)
  {
      echo '&lt;p&gt;&lt;strong&gt;Cannot generate message file&lt;/strong&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;';
      exit;
  } 
  
  $outputstring  = 'Hello, i have been generated by the button';
  
  fwrite($fp, $outputstring);
  }
  
  ?&gt;
  
  &lt;html&gt;
  
  &lt;head&gt;
  &lt;title&gt;Button Writer&lt;/title&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
  &lt;button type="button" onclick="buttonWrite()"&gt;Write to File&lt;/button&gt;
  &lt;/body&gt;
  
  &lt;/html&gt;
  </code></pre>
  
  <p>UPDATE!
  None of these answers actually work at all.. Not sure if its the server im hosting on or what, but yeah the just dont work.. any ideas guys ? </p>