用php怎么做一個簡單的留言頁面?
時間:2023-08-09 19:06:01 | 來源:網(wǎng)站運營
時間:2023-08-09 19:06:01 來源:網(wǎng)站運營
用php怎么做一個簡單的留言頁面?:1、首先構(gòu)建出一個留言頁面的框架
<!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>內(nèi)容</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>
如圖所示
2、點擊提交后,用一個PHP文件去接收數(shù)據(jù),并對內(nèi)容的錄入做判斷
<?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 '留言失敗';
}
}
?>
3、通過點擊“增加留言”/“查看留言”進行接下來的操作,比如,點擊“查看留言”就可以看到自己提交的留言
<?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>內(nèi)容</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)——獲取括號內(nèi)變量的值并賦值給一個新的變量或數(shù)組;$arr[]=array();--一定要加‘[]’foreache($數(shù)組名 as $名){...} 對數(shù)組進行遍歷,并按{...}方法體中的要求格式展示出來