質問編集履歴

4

form_test3\.incにパラメータ初期化を加えました。

2017/04/13 17:33

投稿

YukaSaku
YukaSaku

スコア52

test CHANGED
File without changes
test CHANGED
@@ -190,6 +190,18 @@
190
190
 
191
191
  */
192
192
 
193
+ //Initialization
194
+
195
+ $first_name = "";//string initialization
196
+
197
+ $middle_name = "";//string initialization
198
+
199
+ $last_name = "";//string initialization
200
+
201
+ $phone = "";//integer string initialization
202
+
203
+
204
+
193
205
  $labels = array( "first_name" => "First Name",
194
206
 
195
207
  "middle_name" => "Middle Name",

3

checkblank_2\.phpの波括弧を出来るだけ削除した

2017/04/13 17:33

投稿

YukaSaku
YukaSaku

スコア52

test CHANGED
File without changes
test CHANGED
@@ -26,7 +26,7 @@
26
26
 
27
27
  <?php
28
28
 
29
- /* Program name: checkblank_2.php
29
+ /* Program name: checkBlankOnly_2.php
30
30
 
31
31
  * Description: Program displays the blank form and checks
32
32
 
@@ -36,7 +36,7 @@
36
36
 
37
37
 
38
38
 
39
- {
39
+
40
40
 
41
41
  foreach($_POST as $field => $value)
42
42
 
@@ -46,324 +46,322 @@
46
46
 
47
47
  echo $name_patt;
48
48
 
49
+
50
+
51
+ if(!empty($value))
52
+
53
+ echo $value;
54
+
55
+ echo $field;
56
+
57
+ echo $name_patt;
58
+
59
+ //name
60
+
61
+
62
+
63
+ $name_patt = "/^[A-Za-z' -]{1,50}$/";
64
+
65
+ $phone_patt = "/^[0-9)(xX -]{7,20}$/";
66
+
67
+ $radio_patt = "/(new|changed)/";
68
+
69
+ if(preg_match("/name/i",$field))
70
+
71
+
72
+
73
+ if(!preg_match($name_patt,$value))
74
+
75
+
76
+
77
+ echo $name_patt;
78
+
79
+ $error_array[] = "$value is not a valid name";
80
+
81
+
82
+
83
+
84
+
85
+
86
+
87
+
88
+
89
+ //phone
90
+
91
+ if(preg_match("/phone/i",$field))
92
+
93
+
94
+
95
+ if(!preg_match($phone_patt,$value))
96
+
97
+
98
+
99
+ $error_array[] = "$value is not a valid phone number";
100
+
101
+
102
+
103
+ // endif phone format check
104
+
105
+ if(preg_match("/status/i",$field))
106
+
107
+
108
+
109
+ if(!preg_match($radio_patt,$value))
110
+
111
+
112
+
113
+ $error_array[] = "$value is not a valid status";
114
+
115
+
116
+
117
+
118
+
119
+
120
+
121
+ $clean_data[$field] = strip_tags(trim($value));
122
+
123
+
124
+
125
+ if(@sizeof($error_array) > 0)
126
+
49
127
  {
50
128
 
51
- if(!empty($value))
52
-
53
- echo $value;
54
-
55
- echo $field;
56
-
57
- echo $name_patt;
58
-
59
- //name
60
-
61
- {
62
-
63
- $name_patt = "/^[A-Za-z' -]{1,50}$/";
64
-
65
- $phone_patt = "/^[0-9)(xX -]{7,20}$/";
66
-
67
- $radio_patt = "/(new|changed)/";
68
-
69
- if(preg_match("/name/i",$field))
70
-
71
- {
72
-
73
- if(!preg_match($name_patt,$value))
74
-
75
- {
76
-
77
- echo $name_patt;
78
-
79
- $error_array[] = "$value is not a valid name";
129
+ $message = "<p style='color: red; margin-bottom: 0;
130
+
131
+ font-weight: bold'>
132
+
133
+ You didn't fill in one or more required fields.
134
+
135
+ You must enter:
136
+
137
+ <ul style='color: red; margin-top: 0;
138
+
139
+ list-style: none' >";
140
+
141
+ /* display list of missing information */
142
+
143
+ foreach($error_array as $value)
144
+
145
+
146
+
147
+ $message .= "<li>$value</li>";
148
+
149
+
150
+
151
+ $message .= "</ul>";
152
+
153
+ echo $message;
154
+
155
+ extract($clean_data);
156
+
157
+ include("form_test3.inc");
158
+
159
+ exit();
160
+
161
+ }
162
+
163
+
164
+
165
+ else
166
+
167
+
168
+
169
+ echo "Data is all okay";
170
+
171
+ include("form_test3.inc");
172
+
173
+
174
+
175
+ ?>
176
+
177
+ ```
178
+
179
+ ```
180
+
181
+ ----------------------form_test3.inc--------------------
182
+
183
+ <?php
184
+
185
+ /* Program name: form_test3.inc
186
+
187
+ * Description: Defines a form that collects a user's
188
+
189
+ * name and phone number.
190
+
191
+ */
192
+
193
+ $labels = array( "first_name" => "First Name",
194
+
195
+ "middle_name" => "Middle Name",
196
+
197
+ "last_name" => "Last Name",
198
+
199
+ "phone" => "Phone");
200
+
201
+ $radios = array( "New", "Changed");
202
+
203
+
204
+
205
+ $submit = "Submit Phone Number";
206
+
207
+ ?>
208
+
209
+ <html>
210
+
211
+ <head>
212
+
213
+ <style type='text/css'>
214
+
215
+ <!--
216
+
217
+ form {
218
+
219
+ margin: 1.5em 0 0 0;
220
+
221
+ padding: 0;
80
222
 
81
223
  }
82
224
 
83
- }
84
-
85
-
86
-
87
- }
88
-
89
- //phone
90
-
91
- if(preg_match("/phone/i",$field))
92
-
93
- {
94
-
95
- if(!preg_match($phone_patt,$value))
225
+ .field {padding-bottom: 1em;}
96
-
226
+
97
- {
227
+ label {
228
+
98
-
229
+ font-weight: bold;
230
+
231
+ float: left;
232
+
233
+ width: 20%;
234
+
235
+ margin-right: 1em;
236
+
99
- $error_array[] = "$value is not a valid phone number";
237
+ text-align: right;
100
238
 
101
239
  }
102
240
 
103
- } // endif phone format check
241
+ .submit {
104
-
105
- if(preg_match("/status/i",$field))
242
+
106
-
107
- {
108
-
109
- if(!preg_match($radio_patt,$value))
243
+ margin-left: 35%;
110
-
111
- {
112
-
113
- $error_array[] = "$value is not a valid status";
114
244
 
115
245
  }
116
246
 
117
- }
247
+ -->
248
+
118
-
249
+ </style>
250
+
119
-
251
+ </head>
252
+
120
-
253
+ <body>
254
+
255
+ <h3>Please enter your phone number below</h3>
256
+
257
+ <input type='hidden' name='submitted' value='yes'>
258
+
259
+
260
+
261
+ <?php
262
+
263
+ /* loop that displays the form */
264
+
265
+ if(isset($_POST['submitted']) and $_POST['submitted'] == "yes")
266
+
267
+ echo "<form action='$_SERVER[PHP_SELF]' method='POST'>";
268
+
269
+ echo "<form action='checkblank_2.php' method='post'>";
270
+
121
- $clean_data[$field] = strip_tags(trim($value));
271
+ foreach($labels as $field => $label)
122
-
123
- }
124
-
125
- if(@sizeof($error_array) > 0)
126
272
 
127
273
  {
128
274
 
129
- $message = "<p style='color: red; margin-bottom: 0;
275
+ echo "<div class='field'><label for='$field'>$label</label>
130
-
131
- font-weight: bold'>
276
+
132
-
133
- You didn't fill in one or more required fields.
277
+ <input id='$field' name='$field' type='text' value='".@$$field."'
134
-
135
- You must enter:
278
+
136
-
137
- <ul style='color: red; margin-top: 0;
138
-
139
- list-style: none' >";
140
-
141
- /* display list of missing information */
142
-
143
- foreach($error_array as $value)
144
-
145
- {
146
-
147
- $message .= "<li>$value</li>";
279
+ size='50%' maxlength='65' /></div>\n";
148
-
149
- }
150
-
151
- $message .= "</ul>";
152
-
153
- echo $message;
154
-
155
- extract($clean_data);
156
-
157
- include("form_test3.inc");
158
-
159
- exit();
160
280
 
161
281
  }
162
282
 
163
-
164
-
165
- else
283
+ echo "<div class='field'>
284
+
166
-
285
+ <input type='radio' name='status' checked='checked'
286
+
287
+ value='new' style='margin-left: 25%'/>$radios[0]
288
+
167
- {
289
+ <input type='radio' name='status'
290
+
168
-
291
+ value='changed' style='margin-left: 1em' />$radios[1]</div>";
292
+
169
- echo "Data is all okay";
293
+ echo "<div class='submit'>
170
-
171
- }
294
+
172
-
173
- include("form_test3.inc");
295
+ <input type='submit' name='phoneButton' value='$submit'></div>";
174
-
175
- }
176
296
 
177
297
  ?>
178
298
 
179
299
 
180
300
 
301
+ </form>
302
+
303
+ </body>
304
+
305
+ </html>
306
+
307
+ ```
308
+
309
+ ```
310
+
311
+ ---------------------display_form.php-----------------
312
+
313
+ <!doctype html>
314
+
315
+ <html>
316
+
317
+ <head>
318
+
319
+ <meta charset="utf-8">
320
+
321
+ <title>Repair Order</title>
322
+
323
+ </head>
324
+
325
+
326
+
327
+ <body>
328
+
181
329
  <?php
182
330
 
183
- /* Program name: form_test3.inc
184
-
185
- * Description: Defines a form that collects a user's
186
-
187
- * name and phone number.
188
-
189
- */
190
-
191
- $labels = array( "first_name" => "First Name",
192
-
193
- "middle_name" => "Middle Name",
194
-
195
- "last_name" => "Last Name",
196
-
197
- "phone" => "Phone");
331
+ ini_set("display_errors","1");
332
+
198
-
333
+ error_reporting(E_ALL);
334
+
335
+ echo "<html>
336
+
337
+ <head><title>SQL Query Sender</title></head>
338
+
339
+ <body>";
340
+
199
- $radios = array( "New", "Changed");
341
+ $host="***********.com";
342
+
200
-
343
+ $user="***********";
344
+
201
-
345
+ $password="**********";
202
-
346
+
203
- $submit = "Submit Phone Number";
347
+ $dbname = "***********_***Exercise";
348
+
349
+ $table = "************";
350
+
351
+
352
+
353
+ include("form_test3.inc");
354
+
355
+
204
356
 
205
357
  ?>
206
358
 
207
- <html>
208
-
209
- <head>
210
-
211
- <style type='text/css'>
212
-
213
- <!--
214
-
215
- form {
216
-
217
- margin: 1.5em 0 0 0;
218
-
219
- padding: 0;
220
-
221
- }
222
-
223
- .field {padding-bottom: 1em;}
224
-
225
- label {
226
-
227
- font-weight: bold;
228
-
229
- float: left;
230
-
231
- width: 20%;
232
-
233
- margin-right: 1em;
234
-
235
- text-align: right;
236
-
237
- }
238
-
239
- .submit {
240
-
241
- margin-left: 35%;
242
-
243
- }
244
-
245
- -->
246
-
247
- </style>
248
-
249
- </head>
250
-
251
- <body>
252
-
253
- <h3>Please enter your phone number below</h3>
254
-
255
- <input type='hidden' name='submitted' value='yes'>
256
-
257
- ```
258
-
259
- ```
260
-
261
- ----------------------form_test3.inc--------------------
262
-
263
- <?php
264
-
265
- /* loop that displays the form */
266
-
267
- if(isset($_POST['submitted']) and $_POST['submitted'] == "yes")
268
-
269
- echo "<form action='$_SERVER[PHP_SELF]' method='POST'>";
270
-
271
- echo "<form action='checkblank_2.php' method='post'>";
272
-
273
- foreach($labels as $field => $label)
274
-
275
- {
276
-
277
- echo "<div class='field'><label for='$field'>$label</label>
278
-
279
- <input id='$field' name='$field' type='text' value='".@$$field."'
280
-
281
- size='50%' maxlength='65' /></div>\n";
282
-
283
- }
284
-
285
- echo "<div class='field'>
286
-
287
- <input type='radio' name='status' checked='checked'
288
-
289
- value='new' style='margin-left: 25%'/>$radios[0]
290
-
291
- <input type='radio' name='status'
292
-
293
- value='changed' style='margin-left: 1em' />$radios[1]</div>";
294
-
295
- echo "<div class='submit'>
296
-
297
- <input type='submit' name='phoneButton' value='$submit'></div>";
298
-
299
- ?>
300
-
301
-
302
-
303
- </form>
304
-
305
359
  </body>
306
360
 
307
361
  </html>
308
362
 
309
363
  ```
310
364
 
311
- ```
312
-
313
- ---------------------display_form.php-----------------
314
-
315
- <!doctype html>
316
-
317
- <html>
318
-
319
- <head>
320
-
321
- <meta charset="utf-8">
322
-
323
- <title>Repair Order</title>
324
-
325
- </head>
326
-
327
-
328
-
329
- <body>
330
-
331
- <?php
332
-
333
- ini_set("display_errors","1");
334
-
335
- error_reporting(E_ALL);
336
-
337
- echo "<html>
338
-
339
- <head><title>SQL Query Sender</title></head>
340
-
341
- <body>";
342
-
343
- $host="***********.com";
344
-
345
- $user="***********";
346
-
347
- $password="**********";
348
-
349
- $dbname = "***********_***Exercise";
350
-
351
- $table = "************";
352
-
353
-
354
-
355
- include("form_test3.inc");
356
-
357
-
358
-
359
- ?>
360
-
361
- </body>
362
-
363
- </html>
364
-
365
- ```
366
-
367
365
  ###試したこと
368
366
 
369
367
  出力されるエラーメッセージがちょっとおかしいので、echoでパラメータにどんなデータが入ってきてるのか見る為、First nameに1234、Middle nameに123と入力し、あとはブランクでSubmitすると、

2

項目ごとにCodeで囲みました。

2017/04/13 15:40

投稿

YukaSaku
YukaSaku

スコア52

test CHANGED
File without changes
test CHANGED
@@ -1,4 +1,4 @@
1
- PHPで簡単なフォームを作りました。
1
+ ```PHPで簡単なフォームを作りました。
2
2
 
3
3
  入力項目はFirst name, Middle name, Last name, Phoneの4つです。
4
4
 
@@ -16,15 +16,13 @@
16
16
 
17
17
 
18
18
 
19
-
19
+ ```
20
20
 
21
21
  ###該当のソースコード
22
22
 
23
- ```PHP
24
-
25
- ここにご自身が実行したソースコードを書いてください
26
-
27
- ```
23
+ ```
24
+
25
+ ---------------checkblank_2.php -----------------------
28
26
 
29
27
  <?php
30
28
 
@@ -256,6 +254,10 @@
256
254
 
257
255
  <input type='hidden' name='submitted' value='yes'>
258
256
 
257
+ ```
258
+
259
+ ```
260
+
259
261
  ----------------------form_test3.inc--------------------
260
262
 
261
263
  <?php
@@ -304,6 +306,10 @@
304
306
 
305
307
  </html>
306
308
 
309
+ ```
310
+
311
+ ```
312
+
307
313
  ---------------------display_form.php-----------------
308
314
 
309
315
  <!doctype html>
@@ -356,6 +362,8 @@
356
362
 
357
363
  </html>
358
364
 
365
+ ```
366
+
359
367
  ###試したこと
360
368
 
361
369
  出力されるエラーメッセージがちょっとおかしいので、echoでパラメータにどんなデータが入ってきてるのか見る為、First nameに1234、Middle nameに123と入力し、あとはブランクでSubmitすると、

1

タグの変更

2017/04/13 14:58

投稿

YukaSaku
YukaSaku

スコア52

test CHANGED
File without changes
test CHANGED
@@ -1,5 +1,3 @@
1
- ###前提・実現したいこと
2
-
3
1
  PHPで簡単なフォームを作りました。
4
2
 
5
3
  入力項目はFirst name, Middle name, Last name, Phoneの4つです。
@@ -8,7 +6,7 @@
8
6
 
9
7
  ```
10
8
 
11
- エラーメッセージ
9
+
12
10
 
13
11
  ```
14
12