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");

原文地址:https://www.cnblogs.com/angelgril/p/3115261.html