質問編集履歴
1
修正依頼の通り、phpファイルに含まれているものは一括りにして、別にあるphpファイルを公開いたしました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -14,11 +14,37 @@
|
|
14
14
|
|
15
15
|
### 該当のソースコード
|
16
16
|
|
17
|
-
|
17
|
+
test.php
|
18
|
-
|
18
|
+
|
19
|
-
```h
|
19
|
+
```php
|
20
|
+
|
20
|
-
|
21
|
+
<?php
|
22
|
+
|
23
|
+
require_once "send.php";
|
24
|
+
|
25
|
+
$regist = getPostData();
|
26
|
+
|
27
|
+
?>
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
<!DOCTYPE html>
|
32
|
+
|
33
|
+
<html>
|
34
|
+
|
35
|
+
<head>
|
36
|
+
|
37
|
+
<meta charset="UTF-8">
|
38
|
+
|
39
|
+
<title>掲示板サンプル</title>
|
40
|
+
|
41
|
+
<link rel="stylesheet" href="test.css">
|
42
|
+
|
43
|
+
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
|
44
|
+
|
45
|
+
</head>
|
46
|
+
|
21
|
-
|
47
|
+
<body>
|
22
48
|
|
23
49
|
<h1>掲示板サンプル</h1>
|
24
50
|
|
@@ -78,11 +104,7 @@
|
|
78
104
|
|
79
105
|
|
80
106
|
|
81
|
-
|
82
|
-
|
83
|
-
```javascript
|
84
|
-
|
85
|
-
|
107
|
+
<script>
|
86
108
|
|
87
109
|
document.getElementById('send').onclick = function(){
|
88
110
|
|
@@ -186,8 +208,134 @@
|
|
186
208
|
|
187
209
|
</script>
|
188
210
|
|
211
|
+
</body>
|
212
|
+
|
213
|
+
</html>
|
214
|
+
|
189
215
|
```
|
190
216
|
|
217
|
+
|
218
|
+
|
219
|
+
send.php
|
220
|
+
|
221
|
+
```php
|
222
|
+
|
223
|
+
<?php
|
224
|
+
|
225
|
+
$errors = [];
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
if($_POST){
|
230
|
+
|
231
|
+
$id = null;
|
232
|
+
|
233
|
+
$name = $_POST["name"];
|
234
|
+
|
235
|
+
$content = $_POST["content"];
|
236
|
+
|
237
|
+
if(!$name){
|
238
|
+
|
239
|
+
$errors[] .= "名前を入力してください";
|
240
|
+
|
241
|
+
}
|
242
|
+
|
243
|
+
if(!$content){
|
244
|
+
|
245
|
+
$errors[] .= "投稿内容を入力してください";
|
246
|
+
|
247
|
+
}
|
248
|
+
|
249
|
+
if(!$errors){
|
250
|
+
|
251
|
+
date_default_timezone_set('Asia/Tokyo');
|
252
|
+
|
253
|
+
$created_at = date("Y-m-d H:i:s");
|
254
|
+
|
255
|
+
//DB接続情報を設定します。
|
256
|
+
|
257
|
+
$pdo = new PDO(
|
258
|
+
|
259
|
+
"mysql:dbname=sample;host=localhost","root","",array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET CHARACTER SET `utf8`")
|
260
|
+
|
261
|
+
);
|
262
|
+
|
263
|
+
//SQLを実行。
|
264
|
+
|
265
|
+
$regist = $pdo->prepare("INSERT INTO post(id, name, content, created_at) VALUES (:id,:name,:content,:created_at)");
|
266
|
+
|
267
|
+
$regist->bindParam(":id", $id);
|
268
|
+
|
269
|
+
$regist->bindParam(":name", $name);
|
270
|
+
|
271
|
+
$regist->bindParam(":content", $content);
|
272
|
+
|
273
|
+
$regist->bindParam(":created_at", $created_at);
|
274
|
+
|
275
|
+
$regist->execute();
|
276
|
+
|
277
|
+
|
278
|
+
|
279
|
+
$res = [];
|
280
|
+
|
281
|
+
$post_data = getPostData();
|
282
|
+
|
283
|
+
$cnt = 0;
|
284
|
+
|
285
|
+
foreach($post_data as $loop){
|
286
|
+
|
287
|
+
$res[$cnt]['id'] = $loop['id'];
|
288
|
+
|
289
|
+
$res[$cnt]['name'] = $loop['name'];
|
290
|
+
|
291
|
+
$res[$cnt]['content'] = $loop['content'];
|
292
|
+
|
293
|
+
$res[$cnt]['created_at'] = $loop['created_at'];
|
294
|
+
|
295
|
+
$cnt++;
|
296
|
+
|
297
|
+
}
|
298
|
+
|
299
|
+
echo json_encode($res);
|
300
|
+
|
301
|
+
}else{
|
302
|
+
|
303
|
+
echo json_encode(['error' => $errors]);
|
304
|
+
|
305
|
+
}
|
306
|
+
|
307
|
+
}
|
308
|
+
|
309
|
+
|
310
|
+
|
311
|
+
function getPostData(){
|
312
|
+
|
313
|
+
//DB接続情報を設定します。
|
314
|
+
|
315
|
+
$pdo = new PDO(
|
316
|
+
|
317
|
+
"mysql:dbname=sample;host=localhost","root","",array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET CHARACTER SET `utf8`")
|
318
|
+
|
319
|
+
);
|
320
|
+
|
321
|
+
//SQLを実行。
|
322
|
+
|
323
|
+
$regist = $pdo->prepare("SELECT * FROM post order by created_at DESC");
|
324
|
+
|
325
|
+
$regist->execute();
|
326
|
+
|
327
|
+
return $regist;
|
328
|
+
|
329
|
+
}
|
330
|
+
|
331
|
+
?>
|
332
|
+
|
333
|
+
```
|
334
|
+
|
335
|
+
|
336
|
+
|
337
|
+
test.css
|
338
|
+
|
191
339
|
```css
|
192
340
|
|
193
341
|
.resend-form{
|