html论坛页面怎么做_用php怎么做一个简单的留言页面?

4ff3c67196d7806cdf6155b6c6b6e084.png

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>

如图所示

e4353bf425e773e649d280a40d176171.png

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 '留言失败';

}

}

?>

309d9740ff4d36f8f3d2f67b699cc8b9.png

3、通过点击“增加留言”/“查看留言”进行接下来的操作,比如,点击“查看留言”就可以看到自己提交的留言

ca45769b73cf54a4d6ed1ebf541afcb8.png

<?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 $名){...} 对数组进行遍历,并按{...}方法体中的要求格式展示出来

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值