やりたい事。
htmlファイルのフォームからテキストファイルに番号<>名前<>コメント<>時刻を保存し、
テキストファイルを読み込み、htmlファイルのフォームの下に番号<>名前<>コメント<>時刻を表示したい。
できないこと。
htmlのなかに組み込まれているphpをうまく表示することができない。
require_onceを使うと、phpファイルの変数をhtmlファイルで使うことができると
かいてあったのでやってみたが出来なかった。
<?php header('Content-Type: text/html; charset=UTF-8'); function h($str) { return htmlspecialchars($str, ENT_QUOTES, 'UTF-8'); } require_once('board.php'); ?> <html> <head> <meta charset="utf-8"/> <title>sample</title> </head> <body> <form method="post" action="board.php">//フォーム <p>名前</p> <input type="text" name="name" placeholder="山田太郎" required="required"> <p>コメント</p> <textarea type="text" name="com" required="required"> </textarea> <!-- <input type="hidden" name="token" value="<?=h(sha1(session_id())) ?>"> --> <input type="submit" value="送信"> </form> <p> <?php foreach ($text_rows as $text_row) {//配列でcount<>name<>comment<>data表示 if($text_row === ""){ continue; } $ret = explode("<>", $text_row); echo $ret[0]; echo $ret[1]; echo $ret[2]; echo $ret[3]; echo "<br>"; } ?> </p> </body> </html>
php
1<?php 2header('Content-Type: text/html; charset=UTF-8'); 3?> 4<html> 5 <head> 6 <meta charset="utf-8"/> 7 <title>sample</title> 8 </head> 9 <body> 10 11 <?php 12 13 function h($str) { 14 return htmlspecialchars($str, ENT_QUOTES, 'UTF-8'); 15 } 16 17 //session_start(); // 1 18 19 $name = (string)filter_input(INPUT_POST, 'name'); //名前フォーム値 文字列にキャスト 20 $comment = (string)filter_input(INPUT_POST, 'com');//コメントフォーム値 フィルタリングする 21 //$token = (string)filter_input(INPUT_POST, 'token'); // トークンセッション 22 23 $fp = fopen('board.txt', 'a+'); 24 if ($_SERVER['REQUEST_METHOD'] === 'POST'/* && sha1(session_id()) === $token */) { // postが送られて来てるか?CSRF対策 25 flock($fp, LOCK_SH); 26 $text_rows = file("board.txt"); 27 $count = count($text_rows);//行数をカウント 28 $add_text = ($count + 1)."<>".$name."<>".trim($comment)."<>".date('H時i分');//追加したい文章 29 array_unshift($text_rows, $add_text); 30 flock($fp, LOCK_EX); 31 fwrite($fp, $add_text."\n"); 32 flock($fp, LOCK_UN); 33 fclose($fp); 34 }else{ 35 echo "エラー"; 36 } 37 38 39 40 41 ?> 42 </body> 43 44</html> 45
テキストファイルにはうまく保存することができていますが、表示がうまいこと行きません。
まだまだ未熟者なので教えていただけると幸いです。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/03/02 04:27