2017-04-09 12 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コードの場所を指定する必要があります。私は自分のHTMLと同じページに私のPHPコードを置き、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

あなたはJSでイベントを提出、およびAjaxを使用してサーバーにデータを送信したり、フェッチ処理することができsave.phpに、このファイルでその設定アクションが

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

あなたもの.txtで新しい行を作成するには、\ r \ nを必要とします。 –

0

を置く最も簡単な方法。次に、サーバー側で、要求をキャッチしてデータをデータベースに保存するか、データを保存するファイルを作成するAPIを作成します。

最高

関連する問題