具有输入信息完整性校验功能,用户输入信息不全时,提示“信息不完整!重新输入。”
一共三个文件,留言信息保存在Guestbook.php文件创建的Guestbook.txt文本文档中。
- Guestbook.html
- Guestbook.php
- GuestbookPrint.php
Guestbook.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>留言本</title>
<script type="text/javascript">
function validate_form(){
var tit=document.getElementsByName('title').item(0).value;
var aut=document.getElementsByName('author').item(0).value;
var con=document.getElementsByName('content').item(0).value;
if (tit==""||aut==""||con=="") {
alert("信息不完整!重新输入。");
return false;
} else{
return true;
}
}
</script>
</head>
<body>
<h1 align="center">我的留言本</h1>
<a href="GuestbookPrint.php">查看留言</a>
<form action="Guestbook.php" method="post" onsubmit="return validate_form()">
<table border="0px" cellspacing="" cellpadding="">
<tr>
<td>标题</td>
<td><input type="text" name="title" value="" /></td>
</tr>
<tr>
<td>作者</td>
<td><input type="text" name="author" value="" /></td>
</tr>
<tr>
<td>内容</td>
<td><textarea cols="50" rows="10" name="content"></textarea></td>
</tr>
<tr>
<td><input type="submit" id="" name="" value="提交" /></td>
<td><input type="reset" value="重置" /></td>
</tr>
</table>
</form>
</body>
</html>
Guestbook.php
<?php
$mydate=date('Y/m/d H:i:s');
$outputstring=$mydate."\t".$_POST['title']."\t".$_POST['author']."\t".$_POST['content']."\n";
$fp=fopen("./Guestbook.txt", "ab");
fwrite($fp, $outputstring);
fclose($fp);
echo "留言发表成功";
echo "<a href=\"GuestbookPrint.php\">查看留言</a>";
?>
GuestbookPrint.php
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>查看留言</title>
</head>
<body>
<a href="Guestbook.html">发送新留言</a>
<?php
$file_path='./Guestbook.txt';
if(file_exists($file_path)){
$file_name=file($file_path);
$file_num=count($file_name);
for($i=0;$i<$file_num;$i++){
$line=explode("\t", $file_name[$i]);
echo "<br />";
echo "<table border=\"0px\" cellspacing=\"0\" cellpadding=\"10px\">";
echo "<tr>
<td>日期:</td>
<td>".$line[0]."</td>
</tr>";
echo "<tr>
<td>标题:</td>
<td>".$line[1]."</td>
</tr>";
echo "<tr>
<td>作者:</td>
<td>".$line[2]."</td>
</tr>";
echo "<tr>
<td>内容:</td>
<td>".$line[3]."</td>
</tr>";
echo "</table>";
echo "<br /><hr />";
}
}else{
echo "无此文件";
}
?>
</body>
</html>