index.php文件代码:
<!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:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title>评论</title>
</head>
<body>
<h3 style="text-align: center">留言列表</h3>
<form action="jump.php" method="post">
<input type="hidden" name="act" value="add"/>
<table style="margin:auto;" border="1px solid black" width="1100px" bgcolor="#64bff2">
<tr>
<td>留言者</td>
<td><input type="text" name="user" /></td>
</tr>
<tr>
<td>标题</td>
<td><input type="text" name="topic" /></td>
</tr>
<tr>
<td>内容</td>
<td><textarea name="content" id="content" cols="30" rows="10"></textarea></td>
</tr>
<tr>
<td>心情</td>
<td height="50px">
<input type="radio" name="xinqing" value="qw_cat_0001.gif" checked="checked"/><img height="150" width="150" src="img/qw_cat_0001.gif" alt="膝盖中箭" />
<input type="radio" name="xinqing" value="qw_cat_0002.gif" /><img height="150" width="150" src="img/qw_cat_0002.gif" alt="我察" />
<input type="radio" name="xinqing" value="qw_cat_0003.gif" /><img height="150" width="150" src="img/qw_cat_0003.gif" alt="你懂的" />
<input type="radio" name="xinqing" value="qw_cat_0004.gif" /><img height="150" width="150" src="img/qw_cat_0004.gif" alt="这真是极好的" />
<input type="radio" name="xinqing" value="qw_cat_0005.gif" /><img height="150" width="150" src="img/qw_cat_0005.gif" alt="给力" />
</td>
</tr>
<tr>
<td colspan="2" style="padding-left:200px;padding:auto;">
<input type="submit" value="提交" style="width:50px;height:30px;" />
<input type="reset" value="重置" style="width:50px;height:30px;"/>
</td>
</tr>
</table>
</form>
</body>
</html>
</html>
jump.php文件代码:
<!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:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title>Document</title>
</head>
<?php
$act= isset($_POST['act'])?$_POST['act']:'';
$user= isset($_POST['user'])?$_POST['user']:'';
$topic= isset($_POST['topic'])?$_POST['topic']:'';
$content= isset($_POST['content'])?$_POST['content']:'';
$xinqing= isset($_POST['xinqing'])?$_POST['xinqing']:'';
$time=date('Y-m-d h:i:s');
$filename='text.txt';
$arr=array();
if(file_exists($filename)&&filesize($filename)>0){
$str=file_get_contents($filename);
$arr=unserialize($str);
}
if($act=='add'){
$arr[] =array(
'user'=>$user,
'topic'=>$topic,
'content'=>$content,
'xinqing'=>$xinqing,
'time'=>$time
);
// print_r($arr);
$arr=serialize($arr);//将数组序列化
//a:4:{s:4:"user";s:6:"小明";s:5:"topic";s:9:"留言板";s:7:"content";s:12:"去哪里?";s:7:"xinqing";s:15:"qw_cat_0002.gif";}
//a表示array数组,4表示数组大小,{}数组内容,里面以分号;隔开。
//内容中: s表示类型,4表示值大小,冒号:后是值(注意,一个汉字字符占3个)
file_put_contents($filename,$arr);
$str=file_get_contents($filename);
$arr=unserialize($str);
}
?>
<body>
<h3 style="text-align: center">评论列表</h3>
<table style="margin:auto;" border="1px solid black" width="1100px" bgcolor="#64bff2">
<tr>
<th>名称</th>
<th>话题</th>
<th>内容</th>
<th>心情</th>
<th>时间</th>
</tr>
<?php
foreach ($arr as $key => $value){
echo '<tr height="80px" style="text-align: center;">';
/* foreach ($value as $key2 => $value2){
echo '<td>'.$value2.'</td>';
} */
echo '<td>'.$value['user'].'</td>';
echo '<td>'.$value['topic'].'</td>';
echo '<td>'.$value['content'].'</td>';
echo '<td><img height="100" width="100" src="img/'.$value['xinqing'].'" alt="你懂的" /></td>';
echo '<td>'.$value['time'].'</td>';
echo '</tr>';
}
?>
</table>
</body>
</html>
工程结构:
访问页面效果:
跳转后: