teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

ちょっとリファクタリング

2018/03/02 09:41

投稿

nnahito
nnahito

スコア2006

answer CHANGED
@@ -20,22 +20,41 @@
20
20
  <!DOCTYPE html>
21
21
  <html lang="ja">
22
22
  <head>
23
- <meta charset="UTF-8">
23
+ <meta charset="UTF-8">
24
24
  </head>
25
25
  <body>
26
+
26
- <form action="mission_1-5-2.php" method="post">
27
+ <form action="mission_1-5-2.php" method="post">
27
- <!--入力フォームの作成-->
28
+ <!--入力フォームの作成-->
28
- <input type="text" name="comment">
29
+ <input type="text" name="comment">
29
- <input type="submit" value="送信">
30
+ <input type="submit" value="送信">
30
- </form>
31
+ </form>
32
+
33
+
31
- <?php
34
+ <?php
35
+
36
+ // フォームから値が送信されてきているかの確認
37
+ if ( isset($_POST['comment']) === true ) {
38
+
39
+ // 送信されてきていたら、変数にその値を代入
32
- $comment=$_POST['comment'];
40
+ $comment = $_POST['comment'];
41
+
33
- var_dump($comment);
42
+ // データを書き出すファイル名を設定
34
- $filename='kadai5.txt';
43
+ $filename = 'kadai5.txt';
44
+
45
+ // ファイルに書き出しを開始(ファイルハンドルの取得)
35
- $fp=fopen($filename,'w');
46
+ $fp = fopen($filename, 'a'); // 追加書き込みモード
47
+
48
+ // データを書きだす
36
- fwrite($fp,"$comment");
49
+ fwrite($fp, $comment);
50
+
51
+ // 書き出し処理の終了(ハンドルの開放)
37
- fclose($fp);
52
+ fclose($fp);
53
+
54
+ }
55
+
38
- ?>
56
+ ?>
57
+
39
58
  </body>
40
59
  </html>
41
60
  ```

1

ついき

2018/03/02 09:41

投稿

nnahito
nnahito

スコア2006

answer CHANGED
@@ -11,4 +11,31 @@
11
11
  $comment=$_POST['comment'];
12
12
  ```
13
13
 
14
- ※「'(シングルクォーテーション)」の全角半角問題
14
+ ※「'(シングルクォーテーション)」の全角半角問題
15
+
16
+
17
+ # 追記
18
+
19
+ ```php
20
+ <!DOCTYPE html>
21
+ <html lang="ja">
22
+ <head>
23
+ <meta charset="UTF-8">
24
+ </head>
25
+ <body>
26
+ <form action="mission_1-5-2.php" method="post">
27
+ <!--入力フォームの作成-->
28
+ <input type="text" name="comment">
29
+ <input type="submit" value="送信">
30
+ </form>
31
+ <?php
32
+ $comment=$_POST['comment'];
33
+ var_dump($comment);
34
+ $filename='kadai5.txt';
35
+ $fp=fopen($filename,'w');
36
+ fwrite($fp,"$comment");
37
+ fclose($fp);
38
+ ?>
39
+ </body>
40
+ </html>
41
+ ```