PHP經典實例教程(文本式留言板)


1、添加界面代碼

 1 <html>
 2     <head>
 3         <title>我的留言板</title>
 4     </head>
 5     <body>
 6         <center>
 7             <?php
 8                 include("common.php");
 9             
10             ?>
11             <form action="addInfo.php" method="post">
12             <table width="400px" border="0" cellspacing="0px" cellpadding="0px">
13                 <tr>
14                     <td align="right">標題:</td>
15                     <td>
16                         <input type="text" name="txtTitle"/>
17                     </td>
18                 </tr>
19                 <tr>
20                     <td align="right">留言者:</td>
21                     <td>
22                     <input type="text" name="txtAuthor"/>
23                     </td>
24                 </tr>
25                 <tr>
26                     <td align="right">留言內容:</td>
27                     <td>
28                     <textarea rows="5" cols="30" name="txtContent"></textarea>
29                     </td>
30                 </tr>
31                 <tr align="center">
32                     <td colspan="2">
33                         <input type="submit" value="添加"/>&nbsp;&nbsp;
34                         <input type="reset" value="重置"/>
35                     </td>
36                 </tr>
37             </table>
38             </form>
39         </center>
40         <?php
41             //大寫Y表示2010年
42             //小寫y表示10年
43             //$date=date("Y-m-d",time());
44             //echo $date;
45         ?>
46     </body>
47 </html>
one

界面:

點擊添加按鈕后執行的代碼

 1 <html>
 2     <head>
 3         <title>我的留言板</title>
 4     </head>
 5     <body>
 6         <center>
 7             <?php
 8                 include("common.php");
 9                 //error_reporting=E_ALL & ~E_NOTICE  這句話的意思是,屏蔽掉此類警告和提示
10             ?>
11             <h2>添加留言</h2>
12             <?php
13             //php中的連接字符串用的符號是.,不想其它語言用的是+
14             //file_put_contents(文件名,將要寫入的字符串)
15                 $title=$_POST["txtTitle"];
16                 $author=$_POST["txtAuthor"];
17                 $content=$_POST["txtContent"];
18                 $ip=$_SERVER["REMOTE_ADDR"];
19                 $addtime=date("Y-m-d",time());
20                 $str="{$title}##{$author}##{$content}##{$ip}##{$addtime}@@@";
21                 $info=file_get_contents("liuyan.txt");
22                 file_put_contents("liuyan.txt",$info.$str);
23                 echo "添加成功!";
24             ?>
25         </center>
26     </body>
27 </html>
two

 

 

2、留言列表顯示 

 1 <html>
 2     <head>
 3         <title>我的留言板</title>
 4         <script>
 5             function del(id){
 6                 if(confirm("確認要刪除嗎")){
 7                     window.location.href="del.php?id="+id;
 8                 }
 9             }
10         </script>
11     </head>
12     <body>
13         <center>
14             <?php
15                 include("common.php");
16             ?>
17             <table width="500px" border="1px" cellspacing="0px" cellpadding="0px">
18                 <tr>
19                     <td align="right">標題</td>
20                     <td align="right">留言人</td>
21                     <td align="right">留言內容</td>
22                     <td align="right">IP地址</td>
23                     <td align="right">留言時間</td>
24                     <td align="right">操作</td>
25                 </tr>
26                 <?php
27                     $info=file_get_contents("liuyan.txt");
28                     //輸出數組
29                     
30                     if($info=="")
31                     {
32                         echo "沒有數據";
33                     }
34                     else{
35                         //rtrim()是除去字符串右側的某字符
36                         $info=rtrim($info,"@");
37                         //將留言信息以@@@的符號拆分成留言數組
38                         $lylist=explode("@@@",$info);
39                         //遍歷留言信息數組,對每條留言做再次拆分
40                         foreach($lylist as $a=>$liu){
41                             $ss=explode("##",$liu);
42                             echo "<tr align='right'>";
43                             echo "<td>{$ss[0]}</td>";
44                             echo "<td>{$ss[1]}</td>";
45                             echo "<td>{$ss[2]}</td>";
46                             echo "<td>{$ss[3]}</td>";
47                             echo "<td>{$ss[4]}</td>";
48                             echo "<td><a href='javascript:del({$a})'>刪除</a></td>";
49                             echo "</tr>";
50                         }
51                     }
52                 ?>
53                 </table>
54         </center>
55     </body>
56 </html>
three

 

3、留言刪除代碼

 1 <html>
 2     <head>
 3         <title>我的留言板</title>
 4         
 5     </head>
 6     <body>
 7         <center>
 8             <?php
 9                 include("common.php");
10             ?>
11             <h2>刪除留言</h2>
12                 <?php
13                     header("Content-Type:text/html;charset=gb2312");
14                     //獲取要刪除的留言的id號
15                     $index=$_GET["id"];
16                     $info=file_get_contents("liuyan.txt");
17                     $info=rtrim($info,"@");
18                     $lylist=explode("@@@",$info);
19                     unset($lylist[$index]);
20                     $newInfo=implode("@@@",$lylist);
21                     file_put_contents("liuyan.txt",$newInfo);
22                     echo "刪除成功!";
23                 ?>
24         </center>
25     </body>
26 </html>
four

 

4、公用模板代碼

1 <h2>我的留言板</h2>
2 <a href="one.php">添加留言</a>|
3 <a href="show.php">查看留言</a>
4 <hr width="90%">
five

 

每個界面都會有相同的導航,所以,可以封裝起來,在每個界面調用就可以了

調用的方法是:include("common.php");


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM