2017-04-09 60 views
1

如何從服務器上的HTML表單保存數據?從服務器上的HTML表單保存數據?

這裏是我的代碼:

<form action="?action=save" name="myform" method="post"> 

<textarea class="textBox" name="mytext"> </textarea> 
<input type="submit" class="save" value="save"/> 

在此先感謝。

回答

0

您將需要更改操作名稱以指向PHP代碼的位置。我將我的PHP代碼放在與我的HTML相同的頁面上,並將操作更改爲save.php(這是我的PHP名稱所在的文件名)。

這是我的save.php文件的一切。

<?php 
 
// Check if form is submitted and we have the fields we want. 
 
if(isset($_POST["mytext"])) 
 
{ 
 
\t $file = "data.txt"; 
 
\t $text = $_POST["mytext"]; 
 

 
\t // This file will create a data.txt file and put whatever is in the POST field mytext into the text and put a new line on the end. 
 
\t // The FILE_APPEND allows you to append text to the file. LOCK_EX prevents anyone else from writing to the file at the same time. 
 
\t file_put_contents($file, $text . "\r\n", FILE_APPEND | LOCK_EX); 
 
} 
 
?> 
 
<!DOCTYPE html> 
 
<html> 
 
<head> 
 
\t <title>Save POST Data</title> 
 
</head> 
 
<body> 
 

 
<form action="save.php" name="myform" method="post"> 
 

 
<textarea class="textBox" name="mytext"></textarea> 
 
<input type="submit" class="save" value="save"/> 
 

 
</body> 
 
</html>

+0

它很好用! –

0

最簡單的方式對save.php並在此文件中的設置操作把

<?php 
    if(isset($_POST)){ 
     file_put_contents('file.txt', json_encode($_POST)); 
    } 
+0

你需要一個\ r \ n建立在.TXT新行了。 –

0

您可以處理JS通過使用Ajax提交事件,並且將數據發送到服務器或獲取。然後在您的服務器端,構建一個API來捕獲請求並將數據存儲在數據庫或任何要存儲數據的文件中。

最好

相關問題