Created
December 20, 2013 12:01
-
-
Save KimiyukiYamauchi/8053818 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php include("common2.php"); ?> | |
| <?php include("formhelpers.php"); ?> | |
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <title>ひよこ掲示板 </title> | |
| <meta charset="utf8"> | |
| <style> | |
| body { | |
| background-color: #f0e6ac; | |
| } | |
| p {padding:5px; margin-left: 50px;} | |
| div.content {border-top: 1px dashed #555; margin-top: 10px;} | |
| ul.error {color:red;} | |
| </style> | |
| </head> | |
| <body> | |
| <?php | |
| // メインのロジック: | |
| // - フォームがサブミットされた場合は検証して処理、 | |
| // - それ以外はフォームの表示 | |
| if($_POST['_submit_check']){ | |
| // サブミットされたデータが正しければ、それを処理 | |
| if($form_errors = validate_form()){ | |
| show_form($form_errors); | |
| }else{ | |
| // サブミットされたデータが検証されれば、それを処理 | |
| $errors = process_form(); | |
| show_form($errors); | |
| } | |
| }else{ | |
| // フォームがサブミットされていなければ表示 | |
| show_form(); | |
| } | |
| function show_form($errors = ''){ | |
| // フォームがサブミットされたら、 | |
| // サブミットされたパラメータからデフォルトを取得 | |
| if($_POST['_submit_check']){ | |
| $default = $_POST; | |
| }else{ | |
| // そうでなければ、独自のデフォルトをセット | |
| } | |
| if($errors){ | |
| $error_text = '<tr><td>以下のエラーが発生しました。入力を確認して下さい。'; | |
| $error_text .= '</td><td><ul class="error"><li>'; | |
| $error_text .= implode('</li><li>', $errors); | |
| $error_text .= '</li></ul></td></tr>'; | |
| }else{ | |
| // エラーがなければ、$error_textはブランクにする | |
| $error_text = ''; | |
| } | |
| // PHP モードを終了して、HTMLタグを表示 | |
| ?> | |
| <h1>ひよこ掲示板 </h1> | |
| <?php print $error_text ?> | |
| <form action="<?php print $_SERVER['SCRIPT_NAME']; ?>" method="post"> | |
| 名前 <br> | |
| <?php input_text('name', $default, 24) ?> | |
| <br> | |
| コメント<br> | |
| <?php input_textarea('comment', $default, 40, 3) ?> | |
| <br> | |
| <input type="submit" name="submit" value=" 書き込み "><br> | |
| <input type="hidden" name="_submit_check" value="1" /> | |
| </form> | |
| <?php | |
| $records = bbs_read(); | |
| foreach ($records as $key => $record) { | |
| ?> | |
| <div class="content" > | |
| <span class="id"><?php print h($key + 1); ?></span> | |
| <span class="name"> | |
| 名前:<?php print h($record["name"]); ?> | |
| </span> | |
| <span class="time"> | |
| <?php | |
| print date("Y 年 m月d日 H 時 i 分 s 秒 ", | |
| intval($record["time"])); | |
| ?> | |
| </span> | |
| <p class="comment"><?php print nl2br(h($record["comment"])); ?> | |
| </p> | |
| </div> | |
| <?php | |
| }// 投稿メッセージ表示のforeachのおわり | |
| }// show_form()のおわり | |
| function validate_form(){ | |
| $errors = array(); | |
| // 名前の入力チェック | |
| $name = trim($_POST["name"]); | |
| if (!mb_strlen($name)) { | |
| $errors["name"] = " 名前が入力されていません"; | |
| } elseif (mb_strlen($name) > 20 ) { | |
| $errors["name"] = " 名前は20文字以内で入力してください "; | |
| } | |
| // コメントの入力チェック | |
| $comment = $_POST["comment"]; | |
| if (!mb_strlen($comment)) { | |
| $errors["comment"] = "コメントが入力されていません"; | |
| } elseif ($comment > 400 ) { | |
| $errors["comment"] = "コメントは400文字以内で入力してください "; | |
| } | |
| return $errors; | |
| } | |
| function process_form(){ | |
| $result = bbs_write($_POST); | |
| if (!$result) { | |
| $errors["result"] = " 書き込みに失敗しました"; | |
| }else{ | |
| $errors = ""; | |
| $_POST = ""; | |
| } | |
| return $errors; | |
| } | |
| ?> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment