回答編集履歴

2

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

2018/03/02 09:41

投稿

nnahito
nnahito

スコア2004

test CHANGED
@@ -42,37 +42,75 @@
42
42
 
43
43
  <head>
44
44
 
45
- <meta charset="UTF-8">
45
+ <meta charset="UTF-8">
46
46
 
47
47
  </head>
48
48
 
49
49
  <body>
50
50
 
51
- <form action="mission_1-5-2.php" method="post">
51
+
52
52
 
53
- <!--入力フォームの作成-->
53
+ <form action="mission_1-5-2.php" method="post">
54
54
 
55
- <input type="text" name="comment">
55
+ <!--入力フォームの作成-->
56
56
 
57
- <input type="submit" value="送信">
57
+ <input type="text" name="comment">
58
58
 
59
- </form>
59
+ <input type="submit" value="送信">
60
60
 
61
- <?php
61
+ </form>
62
62
 
63
- $comment=$_POST['comment'];
63
+
64
64
 
65
- var_dump($comment);
65
+
66
66
 
67
- $filename='kadai5.txt';
67
+ <?php
68
68
 
69
- $fp=fopen($filename,'w');
69
+
70
70
 
71
- fwrite($fp,"$comment");
71
+ // フォームから値が送信されてきているかの確認
72
72
 
73
- fclose($fp);
73
+ if ( isset($_POST['comment']) === true ) {
74
74
 
75
+
76
+
77
+ // 送信されてきていたら、変数にその値を代入
78
+
79
+ $comment = $_POST['comment'];
80
+
81
+
82
+
83
+ // データを書き出すファイル名を設定
84
+
85
+ $filename = 'kadai5.txt';
86
+
87
+
88
+
89
+ // ファイルに書き出しを開始(ファイルハンドルの取得)
90
+
91
+ $fp = fopen($filename, 'a'); // 追加書き込みモード
92
+
93
+
94
+
95
+ // データを書きだす
96
+
97
+ fwrite($fp, $comment);
98
+
99
+
100
+
101
+ // 書き出し処理の終了(ハンドルの開放)
102
+
103
+ fclose($fp);
104
+
105
+
106
+
107
+ }
108
+
109
+
110
+
75
- ?>
111
+ ?>
112
+
113
+
76
114
 
77
115
  </body>
78
116
 

1

ついき

2018/03/02 09:41

投稿

nnahito
nnahito

スコア2004

test CHANGED
@@ -25,3 +25,57 @@
25
25
 
26
26
 
27
27
  ※「'(シングルクォーテーション)」の全角半角問題
28
+
29
+
30
+
31
+
32
+
33
+ # 追記
34
+
35
+
36
+
37
+ ```php
38
+
39
+ <!DOCTYPE html>
40
+
41
+ <html lang="ja">
42
+
43
+ <head>
44
+
45
+ <meta charset="UTF-8">
46
+
47
+ </head>
48
+
49
+ <body>
50
+
51
+ <form action="mission_1-5-2.php" method="post">
52
+
53
+ <!--入力フォームの作成-->
54
+
55
+ <input type="text" name="comment">
56
+
57
+ <input type="submit" value="送信">
58
+
59
+ </form>
60
+
61
+ <?php
62
+
63
+ $comment=$_POST['comment'];
64
+
65
+ var_dump($comment);
66
+
67
+ $filename='kadai5.txt';
68
+
69
+ $fp=fopen($filename,'w');
70
+
71
+ fwrite($fp,"$comment");
72
+
73
+ fclose($fp);
74
+
75
+ ?>
76
+
77
+ </body>
78
+
79
+ </html>
80
+
81
+ ```