LoginSignup
0
0

More than 1 year has passed since last update.

複数のテキストフォームの内容をファイルに保存する

Last updated at Posted at 2021-11-06

概要

1つのフォームタグ内にテキストフォームが複数ある場合、PHPに渡してテキストファイルに保存する。

html

ファイル名はなんでもいい。

<form method="post" action="data.php">
  <input type="text" name="name0">
  <br>
  <input type="text" name="name1">
  <br>
  <input type="submit" value="送信">
</form>

PHP

PHPは上記フォームのaction=" "内のファイル名で保存する。
この場合だとdata.phpで保存する。

<?php
    $file_name="data.txt"; //ファイル名
    $file = fopen($file_name,"w");
    flock($file, LOCK_EX);

    $data1 = $_POST["name0"];
    $data2 = $_POST["name1"];

      fputs($file, $data1);
      fputs($file,"\n");
      fputs($file, $data2);
      fputs($file,"\n");

    flock($file, LOCK_UN);
    fclose($file);
?>

参考文献

・PHPによるWebアプリケーションスーパーサンプル第2版
・速攻!図解プログラミングPHP+MySQL

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0