title: How to POST a table to PHP and save it as a text file
body: <p>I have the following program for a form data.
  I have text boxes to input values. after I input those when I click 'Submit' it should display the input values below and the text box should get reset. I can enter another set of inputs and can submit it. It should display below the previous one. 
  Now here's the actual problem I am facing. There is a save button. When I click it, the table input should be saved into my PC using PHP.. But it is not working. Instead of saving the file, it adds another row below.. </p>
  
  <p>Below is my html and PHP codes. Please help..! </p>
  
  <p>HTML</p>
  
  <pre><code>&lt;!doctype html&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta charset="utf-8"&gt;
  &lt;title&gt;Form download&lt;/title&gt;
  &lt;script type="text/javascript" src="jquery-2.1.4.js"&gt;&lt;/script&gt;
  &lt;style&gt;
  .results {
      width: 600px;
      margin-top: 30px;
      text-align: center;
      border: 1px solid #999;
  }
  &lt;/style&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;form name="form"  method="POST" action="excel.php" &gt; 
    &lt;table&gt;
      &lt;tr&gt;
        &lt;td style="text-align:left"&gt;Option 1&lt;/td&gt;
        &lt;td&gt;&lt;input type="text" id="text" name="option1" size="20"&gt;&lt;/td&gt;
  
        &lt;td style="text-align:left"&gt;Option 2&lt;/td&gt;
        &lt;td&gt;&lt;input type="text" id="text" name="option2" size="20"&gt;&lt;/td&gt;
       &lt;/tr&gt;
  
        &lt;/table&gt;
    &lt;br/&gt;
    &lt;input type="submit" value="save" &gt;
    &lt;div class="buttonHolder"&gt;
   &lt;input type="submit" name="submit" value="submit" onClick="insertData()" id="btn_s" /&gt;
          &lt;input value="reset" type="reset" id="btn_i" /&gt; 
      &lt;/div&gt;
  
  &lt;/form&gt; 
      &lt;script&gt;
  
  $(function insertData() {
    $('form[name="form"]').submit(function(e) {
    e.preventDefault();
  
           var name  = $('input[name="option1"]').val(),
           sName = $('input[name="option2"]').val();
  
         $('body').append('&lt;table class="results"&gt;'
                            +'&lt;tr&gt;'
                              +'&lt;th&gt;Option 1&lt;/th&gt;'
                              +'&lt;th&gt;Option 2&lt;/th&gt;'
  
                            +'&lt;/tr&gt;'
                            +'&lt;tr&gt;' 
                              +'&lt;td&gt;'+name+'&lt;/td&gt;'
                              +'&lt;td&gt;'+sName+'&lt;/td&gt;'
                                 +'&lt;/tr&gt;'
  
                      + '&lt;/table&gt;');
    // $('input[type="reset"]').click(function() {
      //  ($('#text').val()).remove(); 
     });
       });
  &lt;/script&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>PHP</p>
  
  <pre><code> &lt;?php
  
  $myfile = fopen("newFile.txt", "w") or die("Unable to open file!");
  $txt= 'Option 1 ' ."\t" .'Option 2 ' ."\r\n" 
   .$_POST['option1'] ."\t"  .$_POST['option2']."\r\n" 
  .$_POST['option3'] ."\t" .$_POST['option4'] ."\r\n".$_POST['option5'] ."\t" .$_POST['option6'] ."\r\n"  ;
  fwrite($myfile, $txt);
  fclose($myfile);
  ?&gt;
  </code></pre>
  
  <p>Please help.. I am stuck with this for 2 days..</p>