1、首先建構出一個留言頁面的架構
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title>Document</title>
</head>
<body>
<h2>添加留言頁面</h2>
<form action="jump.php" method="get">
<input type="hidden" name="act" value="add" />
<table width="80%" cellpadding="0" cellspacing="0" bgcolor="orange">
<tr>
<td>留言者</td>
<td><input type="text" name="username" placeholder="請輸入您的昵稱"/></td>
</tr>
<tr>
<td>标題</td>
<td><input type="text" name="title" placeholder="請輸入您的标題"/></td>
</tr>
<tr>
<td>内容</td>
<td><textarea name="content" id="" cols="30" rows="10"></textarea></td>
</tr>
<tr>
<td>心情</td>
<td><input type="radio" name="xinqing" value="1.png" checked="checked" /><img width="50" height="50" src="picture/1.png" alt="" />
<input type="radio" name="xinqing" value="2.png" /><img width="50" height="50" src="picture/2.png" alt="" />
<input type="radio" name="xinqing" value="3.png" /><img width="50" height="50" src="picture/3.png" alt="" />
<input type="radio" name="xinqing" value="4.png" /><img width="50" height="50" src="picture/4.png" alt="" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" /></td>
</tr>
</table>
</form>
</body>
</html>
如圖所示
2、點選送出後,用一個PHP檔案去接收資料,并對内容的錄入做判斷
<?php
$username=isset($_GET['username'])?$_GET['username']:'';
$title=isset($_GET['title'])?$_GET['title']:'';
$content=isset($_GET['content'])?$_GET['content']:'';
$xinqing=isset($_GET['xinqing'])?$_GET['xinqing']:'';
$act=isset($_GET['act'])?$_GET['act']:'';
$time=date('Y-m-d h:i:s');
$filename='text.txt';
if(file_exists($filename)&&filesize($filename)>0){
$str=file_get_contents($filename);
$arr=unserialize($str);
}
if($act=='add'){
$arr[]=array(
'username'=>$username,
'title'=>$title,
'content'=>$content,
'xinqing'=>$xinqing,
'time'=>$time
);
$arr=serialize($arr);
if(file_put_contents($filename, $arr)){
echo '留言成功'.'<br/>'.'<a href="addmes.php" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" >增加留言</a>|<a href="lookpage.php" target="_blank" rel="external nofollow" >檢視留言</a>';
}else{
echo '留言失敗';
}
}
?>
3、通過點選“增加留言”/“檢視留言”進行接下來的操作,比如,點選“檢視留言”就可以看到自己送出的留言
<?php
$filename='text.txt';
if(file_exists($filename)&&filesize($filename)>0){
$str=file_get_contents($filename);
$arr=unserialize($str);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title>Document</title>
</head>
<body>
<h3>留言檢視頁<a href="addmes.php" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" > 增加留言</a></h3>
<table width="80%" cellpadding="0" cellspacing="0" bgcolor="Cyan">
<tr>
<td>留言者</td>
<td>标題</td>
<td>内容</td>
<td>心情</td>
<td>時間</td>
</tr>
<?php
foreach ($arr as $val){
?>
<tr>
<td><?php echo $val['username']?></td>
<td><?php echo $val['title']?></td>
<td><?php echo $val['content']?></td>
<td><img width="80" height="80" src="picture/<?php echo $val['xinqing']?>" alt="" /></td>
<td><?php echo $val['time']?></td>
</tr>
<?php
}
?>
</table>
</body>
</html>
$arr=file_get_contents($filename)——擷取括号内變量的值并指派給一個新的變量或數組; $arr[]=array();--一定要加‘[]’ foreache($數組名 as $名){...} 對數組進行周遊,并按{...}方法體中的要求格式展示出來