PHP留言板无数据库版

---恢复内容开始---

今天写完作业研究一个留言板,起初主要构想是在PHP和HTML分别写一个HTML用于首页表单样式,PHP用于处理留言程序,先看下HTML首页吧!

我先讲在index.php文件中植入HTML的表单,然后在头部处理留言内容。

处理完了以后再一下表格中显示。

主要思路是将留言内容以数组的形式保存到文件中。

<?php
$a = file_get_contents('./ly.txt');
$aa = json_decode($a,true);
date_default_timezone_set("Asia/Shanghai");
?>
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>留言板</title>
</head>
<body>
	<center><h1>留言板</h1></center>
	<hr >

	<table border="1" cellspacing="0" align="center" width="600">
		<form action="ly.php" method="post">
			<tr>
				<th>姓名</th>
				<td>
					<input type="text" name="xm">
				</td>
			</tr>
			<tr>
				<th>标题</th>
				<td>
					<input type="text" name="bt">
				</td>
			</tr>
			<tr>
				<th>内容</th>
				<td>
					<textarea name='nr' rows=5 cols=70 placeholder="请填写留言内容!"></textarea>
				</td>
			</tr>
			<tr>
				<th colspan="2">
					<input type="hidden" name="sj" value="<?= date("Y/m/d h:i:sa")?>">
					<input type="submit" name="提交">
					<input type="reset" name="">
				</th>
			</tr>
		</form>
	</table>
	<hr >
	<table border="1" cellspacing="0" width="1000" align="center">
		<caption>留言内容</caption>
		<tr>
			<th width="100">姓名</th>
			<th width="120">标题</th>
			<th>内容</th>
			<th width="100">时间</th>
			<th width="100">操作</th>
		</tr>
			<?php foreach($aa as $k => $v): ?>
				<tr>
					<td align="center"><?= $v['xm'] ?></td>
					<td align="center"><?= $v['bt'] ?></td>
					<td><?= $v['nr'] ?></td>
					<td><?= $v['sj'] ?></td>
					<td align="center">
						<a href='./sc.php?id=<?= $k ?>'>删除</a>  
						<a href='./xg.php?id=<?= $k ?>'>修改</a>
					</td>

				</tr>

			<?php endforeach ?>
		
	</table>
</body>
</html>

  表单做好了下面就该写留言板处理的程序了。

  这段代码主要是将文件中的数据提取出来,然后处理。

<?php
if(empty($_POST['bt']) || empty($_POST['xm']) || empty($_POST['nr'])){
	die('对不起,您没有输入不能提交');
} 
$ly = file_get_contents ('./ly.txt');
$lyy = json_decode($ly,true);
$lyy[] = $_POST;
$lyyy = json_encode($lyy);
file_put_contents('./ly.txt',$lyyy);
echo '留言成功2秒后返回';
header('refresh:2;url=./index.php');
?>

  下面是修改的代码

<?php
$a=file_get_contents('./ly.txt');
$aa=json_decode($a,true);
$k= $_GET['id'];
date_default_timezone_set("Asia/Shanghai");
?>


<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>留言板——修改</title>
</head>
<body>
	<center><h1>留言板——修改</h1></center>
	<hr >

	<table border="1" cellspacing="0" align="center" width="600">
		<form action="xgcx.php" method="post">
			<tr>
				<th>姓名</th>
				<td>
					<input type="text" name="xm" value="<?= $aa[$k]['xm']; ?>">
				</td>
			</tr>
			<tr>
				<th>标题</th>
				<td>
					<input type="text" name="bt" value="<?= $aa[$k]['bt']; ?>">
				</td>
			</tr>
			<tr>
				<th>内容</th>
				<td>
					<textarea name='nr' rows=5 cols=70 placeholder="请填写留言内容!"><?= $aa[$k]['nr']; ?></textarea>
				</td>
			</tr>
			<tr>
				<th colspan="2">
					<input type="hidden" name="id" value="<?= $k?>">
					<input type="hidden" name="sj" value="<?= date("Y/m/d h:i:sa")?>">
					<input type="submit" value="修改" >
					<input type="reset" name="">
				</th>
			</tr>
		</form>
	</table>
	</body>
</html>

  

<?php

if(empty($_POST['bt']) || empty($_POST['xm']) || empty($_POST['nr'])){
	die('对不起,您没有输入不能提交');
} 
$ly = file_get_contents ('./ly.txt');
$lyy = json_decode($ly,true);
$lyy[$_POST['id']] = $_POST;
$lyyy = json_encode($lyy);
file_put_contents('./ly.txt',$lyyy);
echo '修改成功2秒后返回';
header('refresh:2;url=./index.php');
?>

  

猜你喜欢

转载自www.cnblogs.com/qzrs/p/9985886.html