第十週Textarea與留言版<?php // 檔案操作 $fp=fopen("test.txt","w"); // 開啟檔案 寫入 for ($i=1; $i<=100; $i++) { fputs($fp,$i); // 寫入檔案 fputs($fp,"\n"); // 跳列 } fclose($fp); // 關檔 $fp=fopen("test.txt","r"); // 開檔 輸入 while ($line=fgets($fp)) { // 讀入檔案,每次一列 echo $line; // 顯示於瀏覽器 echo "<br>"; // 跳列 }; fclose($fp); // 關檔 ?> <?php // 留言板 $command=$_POST["command"]; if ($command==null) { echo("<html> <body> <center> <form method=post action=$SELF_PHP> 留言版: <p> <textarea name='note' rows=3 cols=40 wrap=physical> </textarea> <p> <input type=submit name=command value=送出> <input type=reset name=command value=清除> </form> </center> </body> </html>"); } if ($command=="送出") { $note=$_POST["note"]; $note = str_replace("\n", "<br>", $note); echo("<html> <body> <center> 留言: <p> $note </center> </body> </html>"); } ?>
作業:利用前述兩個範例, 寫一個留言版, 將留言內容寫入一個檔案
|