第十二週 表單 - 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>"); } ?>
作業 : 整合前述兩個範例, 第一個頁面有三個命令按鈕, 一為留言, 一為存檔, 一為讀取留言 留言是將內容顯示於螢幕, 存檔是將留言儲存於檔案, 讀取留言是讀取先前存檔的留言 再顯示於螢幕
|