こんにちは。PHP初心者です。
現在、PHPを使ったHTMLの投稿フォームを作成しています。
この投稿フォームでは、新規投稿、削除、編集が出来ることを目指しています。投稿フォームは投稿番号、投稿者名、コメント、日時、パスワードから成っており(投稿番号と日時は自動生成)、この順番の通りにテキストファイルに保存されます。
結論から言えば、現状のソースコードで新規投稿、編集、削除全て行えています。しかし、問題がテキストボックスのinput typeをhiddenに指定する時に起こります。
と言うのも、この投稿フォームでは編集したい番号を投稿者自ら指定し一旦送信した後、投稿フォームにその編集番号の投稿者名とコメントが自動的に入るようにしています。そして、現状は指定した編集番号がしっかりと入っているかどうか確かめるために、
<input type = "number" name = "editor" value = "<?php echo $editnumber; ?>">
としてテキストボックス内に表示させています。そして次のステップとしてこのテキストボックスを見えないようにしたいのですが、そのためにinput typeをnumberからhiddenに変えると、投稿フォームの内容がテキストファイルに保存されません。
hiddenにすると$_POST["editor"]の型が変わるのかもと考えてみたのですが、いまいちわかりません。
原因と解決策をご教授していただけると嬉しいです。
当該箇所のソースコード
PHP
1<h1>入力フォーム</h1><br /> 2 <form action = "" method = "post"> 3 <h2>編集番号</h2> 4 <input type = "number" name = "editor" value = "<?php echo $editnumber; ?>"> #question.この箇所のinput typeをhiddenに変えたい 5 <h2>名前</h2> 6 <input type = "text" name = "name" value = "<?php echo $editname; ?>"/> 7 <h2> コメント</h2> 8 <input type = "text" name = "comment" value = "<?php echo $editcomment; ?>"/> 9 <h2>パスワード</h2> 10 <input type = "text" name = "password"/> 11 <br /> 12 <input type = "submit" value = "送信" /><br /> 13 </form> 14
ソースコード全体
PHP
1#投稿フォームで入力→送信→テキスト保存 2 <?php 3 if(file_exists("mission.txt")){ #テキストファイルが無ければ作成。あればそのまま投稿処理 4 $get = file("mission.txt"); 5 foreach($get as $use){ 6 $list = explode("<>", $use); 7 $toukounumber = $list[0]+1; 8 } 9 if($get == null){ 10 $toukounumber = 1; 11 } 12 13 $editname = ""; 14 $editcomment = ""; 15 16 17 if(isset($_POST["name"]) and isset($_POST["comment"])){ #投稿処理 18 #機能してる 19 if($_POST["editor"] != true){ #新規投稿モード 20 21 $nichizi = date("Y/m/d H:i:s"); 22 $name = $_POST["name"]; 23 $comment = $_POST["comment"]; 24 $password = $_POST["password"]; 25 $chain = $toukounumber."<>".$name."<>".$comment."<>".$nichizi."<>".$password."<>"; 26 $fp = fopen("mission.txt", "a"); 27 fwrite($fp, $chain.PHP_EOL); 28 fclose($fp); 29 $toukounumber += 1; 30 } 31 32 if($_POST["editor"] == true){ #編集モード 33 $get = file("mission.txt"); 34 $fp = fopen("mission.txt", "w"); 35 fwrite($fp, ""); 36 fclose($fp); 37 $fp = fopen("mission.txt", "a"); 38 $nichizi = date("Y/m/d H:i:s"); 39 $name = $_POST["name"]; 40 $comment = $_POST["comment"]; 41 $password = $_POST["password"]; 42 $chain = $_POST["editor"]."<>".$name."<>".$comment."<>".$nichizi."<>".$password."<>"; 43 foreach($get as $use){ 44 $list = explode("<>", $use); 45 if($list[0] == $_POST["editor"]){ 46 fwrite($fp, $chain.PHP_EOL); 47 } 48 else{ 49 fwrite($fp, $use); 50 } 51 } 52 fclose($fp); 53 } 54 } 55 56 if(isset($_POST["delete"])){ 57 $get = file("mission.txt"); 58 foreach($get as $use){ 59 $list = explode("<>", $use); 60 if($list[0] == (int)$_POST["delete"]){ 61 if($list[4] == $_POST["deletepassword"]){ 62 $fp = fopen("mission.txt", "w"); 63 fwrite($fp, ""); 64 fclose($fp); 65 $fp = fopen("mission.txt", "a"); 66 foreach($get as $use2){ 67 $list2 = explode("<>", $use2); 68 if($list2[0] != (int)$_POST["delete"]){ 69 fwrite($fp, $use2); 70 } 71 } 72 fclose($fp); 73 $get2 = file("mission.txt"); 74 foreach($get2 as $use3){ 75 $list3 = explode("<>", $use3); 76 $toukounumber = $list3[0]+1; 77 } 78 } 79 } 80 } 81 } 82 83 if(isset($_POST["edit"])){ 84 $get = file("mission.txt"); 85 foreach($get as $use){ 86 $list = explode("<>", $use); 87 if($list[0] == (int)$_POST["edit"]){ 88 if($list[4] == $_POST["editpassword"]){ 89 $editnumber = $list[0]; 90 $editname = $list[1]; 91 $editcomment = $list[2]; 92 } 93 } 94 } 95 } 96 } 97 else{ 98 $toukounumber = 1; 99 $editname = ""; 100 $editcomment = ""; 101 102 $ff = fopen("mission.txt", "w"); 103 fwrite($ff, null); 104 fclose($ff); 105 } 106?> 107 108 109<html> 110 <head> 111 <title>削除機能</title> 112 </head> 113 <body> 114 115 <h1>入力フォーム</h1><br /> 116 <form action = "" method = "post"> 117 <h2>編集番号</h2> 118 <input type = "number" name = "editor" value = "<?php echo $editnumber; ?>"> #question.この箇所のinput typeをhiddenに変えたい 119 <h2>名前</h2> 120 <input type = "text" name = "name" value = "<?php echo $editname; ?>"/> 121 <h2> コメント</h2> 122 <input type = "text" name = "comment" value = "<?php echo $editcomment; ?>"/> 123 <h2>パスワード</h2> 124 <input type = "text" name = "password"/> 125 <br /> 126 <input type = "submit" value = "送信" /><br /> 127 </form> 128 <br /> 129 130 <h1>削除番号指定用フォーム</h1> 131 <form action = "" method = "post"> 132 <h2>削除対象番号</h2> 133 <input type = "number" name = "delete" /> 134 <h2>パスワード</h2> 135 <input type = "text" name = "deletepassword"/> 136 <br /> 137 <input type = "submit" value = "削除" /> 138 </form> 139 <br /> 140 141 <h1>編集番号指定用フォーム</h1> 142 <form action = "" method = "post"> 143 <h2>編集対象番号</h2> 144 <input type = "number" name = "edit" /> 145 <h2>パスワード</h2> 146 <input type = "text" name = "editpassword"/> 147 <br /> 148 <input type = "submit" value = "編集" /> 149 </form> 150 151 </body> 152</html>
回答2件
あなたの回答
tips
プレビュー