留言板项目
输入页部分(html)
文件名: liuyan.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>添加留言</title> //设置一个主标题
</head>
<h1 align="center">留言板</h1>
<body>
<center>
<a href="index.php">查看留言</a>
<hr width="500px">
//设置一个输出框 并且以post方式输出到save.php
<form action="save.php" method="POST">
标题:<input type="text" name="title"><br>
作者:<input type="text" name="author"><br>
留言内容:<textarea cols="30" rows="5" style="resize: none" name="content"></textarea><br>
提交:<input type="submit" value="提交留言"><br>
</form>
</center>
</body>
</html>
写入文件部分(php)
文件名: save.php
<?php
$fg = file_get_contents('ly.txt');
$addr = $_SERVER['REMOTE_ADDR'];
$_POST['IP'] = $addr;
date_default_timezone_set('PRC');
$time = date('Y-m-d H:i:s', time());
$_POST['time'] = $time;
if($fg) {
$arr = json_decode($fg, true);
array_push($arr, $_POST);
}else{
$arr = [];
$arr[] = $_POST;
}
$rs = json_encode($arr);
file_put_contents('ly.txt',$rs);
echo "<script>alert('添加成功,即将跳转');location.href='index.php'</script>";
?>
<?php
$fg = file_get_contents('ly.txt');
$addr = $_SERVER['REMOTE_ADDR'];
$_POST['IP'] = $addr;
date_default_timezone_set('PRC');
$time = date('Y-m-d H:i:s', time());
$_POST['time'] = $time;
if($fg){
$arr = json_decode($fg, true);
array_push($arr,$_POST);
}else {
$arr = [];
$arr[] = $_POST;
}
回显查看部分(php)
文件名: index.php
<?php
$res = file_get_contents('ly.txt');
$arr = json_decode($res,true);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>查看留言</title>
<style>
.dd{
text-align: center;
}
</style>
</head>
<body>
<center>
<a href="liuyan.html">添加留言</a>
<hr width="500px">
<table border='1' width='500px' height='300px' align='center'>
<caption><h2>留言查询</h2></caption>
<tr>
<th>序号</th>
<th>标题</th>
<th>作者</th>
<th>IP地址</th>
<th>内容</th>
<th>操作</th>
</tr>
<?php
global $abc;
foreach ($arr as $k => $v):
?>
<tr>
<td><?= $k+1; ?></td>
<td><?= $v['title']; ?></td>
<td><?= $v['author']; ?></td>
<td><?= $v['IP']; ?></td>
<td><?= $v['content']; ?></td>
<td>
<a href="del.php?id=<?= $k ?>">删除</ a>
</td>
</tr>
<?php
endforeach;
?>
<?php
foreach ($arr as $k => $value) {
$k += 1;
echo $k;
echo "<tr>";
echo "<td> {$k} </td>
<td>{$value['title']}</td>
<td>{$value['author']}</td>
<td>{$value['IP']}</td>
<td>{$value['content']}</td>
";
echo " <td>
<a href='del.php?id=$k'>删除</a>
</td>";
echo "</tr>";
}
?>
<?php
$i = 1;
foreach ($arr as $k => $v) {
echo "<tr align='center'><td>$i</td>";
foreach ($v as $kk => $vv) {
echo "
<td>$vv</td>";
}
echo "<td><a href='del.php?id=$i'>删除</a></td>
</tr>";
$i++;
}
?>
</table>
</center>
</body>
</html>
删除操作部分(php)
<?php
$va = $_GET[id]-1;
$a = file_get_contents('ly.txt');
$r = json_decode($a,true);
unset($r[$va]);
sort($r);
$r = array_values($r);
$rs = json_encode($r);
file_put_contents('wd.txt',$rs);
echo "<script>alert('删除成功,即将跳转');location.href='index.php'</script>";
?>