title: How to write to text file using onClick?
body: <p>I created a simple php/javascript/html page. </p>
  
  <p>It works like this:</p>
  
  <ul>
  <li>It gets the information from an SQL Query;</li>
  <li>It uses a <code>for</code> to display all the information on a table;</li>
  <li>At each line, there are two buttons. Each button calls a function, that writes a message in the text file, along with the information on that particular line.</li>
  </ul>
  
  <p>My problem:
  Whenever I refresh the page, instead of waiting for me to click a button and then write the message, everything is being written in the file.</p>
  
  <p>I have already tried to take the function calls to outside of the <code>for</code> loop, but that didn't solve. I believe that, since they are inside the loop, every time I refresh the page, both buttons "are clicked".</p>
  
  <p>I also tried to change the type from button to submit, but it didn't work.</p>
  
  <p>Here is the code that I'm using it:</p>
  
  <pre><code>&lt;?php   
  for($i=0;$i&lt;count($aux);$i++){
  ?&gt;
   &lt;tr&gt;
   &lt;td&gt;&lt;?php echo $i+1; // Simple counter, to have everything in order ?&gt;&lt;/td&gt;
   &lt;td&gt;&lt;?php echo $aux[$i]['NAME']; // Displays the name?&gt;&lt;/td&gt;
   &lt;td&gt;&lt;?php echo $aux[$i]['INFO']; // Display some information?&gt;&lt;/td&gt;
  
  &lt;script&gt;
  
  function First_Message(){
   &lt;?php  // If the first button is pressed, then this function is called
   $contents .= "Message: ".trim($aux[$i]['NAME'])." message #1 ".date("H:i:s");
   $success = file_put_contents("file.txt", $contents);
   ?&gt;
   }
  &lt;/script&gt;
  
  &lt;script&gt;
  function Second_Message(){
   &lt;?php  If the second button is pressed, then this function is called
   $contents .= "Please,: ".trim($aux[$i]['NAME'])." message #2 ".date("H:i:s");
   $success = file_put_contents("file.txt", $contents);
   ?&gt;
   }
  &lt;/script&gt;
  
  &lt;!-- Creates the first button --&gt;
  
  &lt;td&gt;&lt;input type="button" value="message_1" name = "balcao" onClick = "First_Message()" /&gt;&lt;/td&gt;
  
  &lt;!-- Creates the second button --&gt;
  
  &lt;td&gt;&lt;input type="button" value="message_2" name = "balanca" onClick = 
  "Second_Message()" /&gt;&lt;/td&gt;
  
  &lt;/tr&gt;
  &lt;?php   
  } // End Loop
  ?&gt;
  
  </code></pre>
  
  <p>I expect the output to be only one line of message being written on the text file, and this message to be what I decide based on the click on any of those two buttons.</p>
  
  <p>If someone could tell me what I'm doing wrong or/and tell me an alternative to what I'm currently doing it with onClick, which I think is the problem, I would really appreciate.</p>
  
  <p>Feel free to ask for more info if you need.</p>