用php怎么做一个简单的留言页面?
时间:2023-08-09 19:06:01 | 来源:网站运营
时间:2023-08-09 19:06:01 来源:网站运营
用php怎么做一个简单的留言页面?: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:lang="en">
<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 border="1" 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">增加留言</a>|<a href="lookpage.php">查看留言</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:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title>Document</title>
</head>
<body>
<h3>留言查看页<a href="addmes.php"> 增加留言</a></h3>
<table border="1" 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 $名){...} 对数组进行遍历,并按{...}方法体中的要求格式展示出来