2015-10-14 26 views
1

我有以下程序的表單數據。 我有文本框來輸入值。在我點擊「提交」後輸入這些內容後,它應該顯示下面的輸入值,並且文本框應該被重置。我可以輸入另一組輸入並可以提交。它應該顯示在前一個之下。 現在,這是我面臨的實際問題。有一個保存按鈕。當我點擊它,表格輸入應該保存到我的電腦使用PHP ..但它不工作。它不會保存文件,它會在下面添加另一行..如何發佈一個表到php並保存爲一個文本文件

下面是我的html和php代碼。請幫忙..!

HTML

<!doctype html> 
<html> 
<head> 
<meta charset="utf-8"> 
<title>Form download</title> 
<script type="text/javascript" src="jquery-2.1.4.js"></script> 
<style> 
.results { 
    width: 600px; 
    margin-top: 30px; 
    text-align: center; 
    border: 1px solid #999; 
} 
</style> 
</head> 
<body> 
<form name="form" method="POST" action="excel.php" > 
    <table> 
    <tr> 
     <td style="text-align:left">Option 1</td> 
     <td><input type="text" id="text" name="option1" size="20"></td> 

     <td style="text-align:left">Option 2</td> 
     <td><input type="text" id="text" name="option2" size="20"></td> 
    </tr> 

     </table> 
    <br/> 
    <input type="submit" value="save" > 
    <div class="buttonHolder"> 
<input type="submit" name="submit" value="submit" onClick="insertData()" id="btn_s" /> 
     <input value="reset" type="reset" id="btn_i" /> 
    </div> 

</form> 
    <script> 

$(function insertData() { 
    $('form[name="form"]').submit(function(e) { 
    e.preventDefault(); 

     var name = $('input[name="option1"]').val(), 
     sName = $('input[name="option2"]').val(); 

     $('body').append('<table class="results">' 
          +'<tr>' 
          +'<th>Option 1</th>' 
          +'<th>Option 2</th>' 

          +'</tr>' 
          +'<tr>' 
          +'<td>'+name+'</td>' 
          +'<td>'+sName+'</td>' 
           +'</tr>' 

        + '</table>'); 
    // $('input[type="reset"]').click(function() { 
    // ($('#text').val()).remove(); 
    }); 
    }); 
</script> 

</body> 
</html> 

PHP

<?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); 
?> 

請幫助..我堅持這2天..

回答

0

它可能是這樣的簡單:

$header = "Option 1 \t Option 2\n"; 
file_put_contents("newFile.txt", $header, FILE_APPEND); 
foreach($_POST as $key => $value) { 
    file_put_contents("newFile.txt", $value."\n", FILE_APPEND); 
} 
+0

謝謝..但我認爲有一個問題,也HTML部分..當我點擊保存按鈕它應該處理PHP部分ryt?但相反,它做'提交'按鈕的一部分..! :( – Arjun

0

在HTML中,您需要更改類型從提交到按鈕的'提交'按鈕。並改變你的腳本 -

$(function insertData() { 
    $('#btn_s').on('click', function(e) { 

    var name = $('input[name="option1"]').val(), 
    sName = $('input[name="option2"]').val(); 

    $('body').append('<table class="results">' 
         +'<tr>' 
         +'<th>Option 1</th>' 
         +'<th>Option 2</th>' 

         +'</tr>' 
         +'<tr>' 
         +'<td>'+name+'</td>' 
         +'<td>'+sName+'</td>' 
          +'</tr>' 

       + '</table>'); 
    // $('input[type="reset"]').click(function() { 
    // ($('#text').val()).remove(); 
    }); 
}); 
+0

我嘗試過,但現在當我點擊提交它指向PHP。表格不顯示在下面。 – Arjun

相關問題