PHP——简单留言板

这是我在学习php以来做的第一个非常小非常小的项目(也可以说不能称之为一个项目),但是却很好的让我掌握了近期学习的内容并且激发了我继续学习的信心和兴趣,接下来讲解一下我做这个简单留言板的步骤以及所用工具。

先看效果图如下:

一、做一个留言板首先就是进行页面的排版,这个步骤可以借助layoutit工具根据自己的需要进行在线排版,之后下载下来根据自己的需要进行修改即可,非常方便。网址为:www.layoutit.cn这个是国内版的,国外版的也能访问,但是特别慢而且是英文,如下图:

二、将下载下来的留言板导入到自己的编辑器中,进行修改。因为还未学习到数据库,所以现在先将数据保存到文件中,代码如下:

<?php
	
	header('content-type:text/html;charset=utf-8');
	date_default_timezone_set('PRC');
	$filename="msg.txt";
	$msgs=[];//创建一个空数组,PHP5.4之后可以用这种形式来代替array创建数组
	if(file_exists($filename)){
		$str=file_get_contents($filename);
		if(strlen($str)>0){
			$msgs=unserialize($str);
		}
	}

// print_r($msgs);

	if(isset($_POST['btnmsg'])){
		$name=$_POST['name'];
		$note=$_POST['note'];
		$time=time();
		$data=compact('name','note','time');
		array_push($msgs,$data);
		$msgs=serialize($msgs);
		if(file_put_contents($filename,$msgs)){
			echo "<script>alert('留言成功');location.href='note.php';</script>";
		}else{
			echo "<script>alert('留言失败');location.href='note.php';</script>";
		}
	}
 ?>

<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<!-- <script type="text/javascript" src="js/bootstrap.min.js"></script>
<script type="text/javascript" src="js/bootstrap.js"></script> -->
<!-- <script type="text/javascript" src="js/bootstrap.min.js"></script> -->

<!-- <script type="text/javascript" src="http://www.francescomalagrino.com/BootstrapPageGenerator/3/js/jquery-2.0.0.min.js"></script>
<script type="text/javascript" src="http://www.francescomalagrino.com/BootstrapPageGenerator/3/js/jquery-ui"></script> -->
<link href="http://www.francescomalagrino.com/BootstrapPageGenerator/3/css/bootstrap-combined.min.css" rel="stylesheet" media="screen">

</head>
<body>
<div class="container-fluid">
	<div class="row-fluid">
		<div class="span12">
			<div class="page-header">
				<h1>
					留言板-<small>v1.0</small>
				</h1>
			</div>
			<div class="hero-unit">
				<h1>
					Hello, world!
				</h1>
				<p>
					这是学习PHP以来的第一个小项目,留言板第一个版本。以后再接再厉把留言板更完美的版本做出来,fighting,fighting,fighting!!!
				</p>
			</div>

			<?php if(is_array($msgs)&&count($msgs)>0):?>
			<table class="table">
				<thead>
					<tr>
						<th>
							编号
						</th>
						<th>
							用户
						</th>
						<th>
							时间
						</th>
						<th>
							内容
						</th>
					</tr>
				</thead>
				<tbody>
					<?php $i=1;foreach($msgs as $val):?>
					<tr>
						<td>
							<?php echo $i++; ?>
						</td>
						<td>
							<?php echo $val['name']; ?>
						</td>
						<td>
							<?php echo date("m/d/Y H:i:s",$val['time']);?>
						</td>
						<td>
							<?php echo $val['note']; ?>
						</td>
					</tr>
					<?php endforeach; ?>
				</tbody>
			</table>
		<?php endif; ?>
			<form class="" action="#" method="post">
				<fieldset>
					 <legend>留言区</legend><br>
					 <label>用户名:</label><input type="text" name="name" required/><br>
					 <label>留言内容:</label><textarea name="note" required rows="8" cols="80"></textarea><br>

					 <button type="submit" name="btnmsg" class="btn btn-primary btn-lg">提交</button>
				</fieldset>
			</form>
		</div>
	</div>
</div>
</body>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

.无名之辈

1毛也是爱~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值