1. 程式人生 > >PHP留言板無資料庫版

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');
?>