<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form>
<table>
<tr>
<td>评论框</td>
<td>
<textarea>
</textarea>
</td>
</tr>
</table>
</form>
</body>
</html>
用PHP来制作评论系统在PHP and mySQL的环境下运行。有三个文件:comments.php, 是用来显示[评论的, commentadd.php, 用来处理评论内容的, and commentform.html 通过FROM来提交评论。
1.首先建立一个数据库,如果已经建立则建立一个符合条件的表:
CREATE TABLE `comtbl` (
`postID` INT NOT NULL AUTO_INCREMENT ,
`postTITLE` TEXT NOT NULL ,
`posterNAME` TEXT NOT NULL ,
`posterEMAIL` TEXT NOT NULL ,
`postTIME` TIMESTAMP NOT NULL ,
`postTXT` TEXT NOT NULL ,
PRIMARY KEY ( `postID` )
)
评论查看页:COMMENTS.PHP,具体内容为(有用户名和密码的在实际工作中要改变):
$dbcnx = mysql_connect("localhost", "username", "password")
mysql_select_db("comments")
接下来需要对表进行查询,并且把ID 按Descending: 顺序排序:
$result = mysql_query("SELECT * FROM comtbl ORDER BY postID DESC")
if (!$result) {
echo("<b>Error performing query: " . mysql_error() . "</b>")
exit()
}
在这里因为要读出好多条记录,所以用循环来读,具体程序如下:
while ($row = mysql_fetch_array($result) ) {
$msgTxt = $row["postTXT"]
$msgId = $row["postID"]
$SigName = $row["posterNAME"]
$SigDate = $row["postTIME"]
$msgTitle = $row["postTITLE"]
$url = $row["posterEMAIL"]
现在到了最关键的一步了,也是困难的一步: 因为在这里用到MySQL's TIMESTAMP 函数 (功能是可以自动的饿把时间添加到一个表中),并且需要取得时间的字符串,使用字符串函数substr() ( $yr 表示年, $mo 表示月, 等等):
$yr = substr($SigDate, 2, 2)
$mo = substr($SigDate, 4, 2)
$da = substr($SigDate, 6, 2)
$hr = substr($SigDate, 8, 2)
$min = substr($SigDate, 10, 2)
还需要对上述代码的功能加以扩充来实现12或24小时表示或者用 AM和PM来表示上下午,代码如下:
if ($hr >"11") {
$x = "12"
$timetype = "PM"
$hr = $hr - 12
}else{
$timetype = "AM"
}
另外,当评论者要是留下Email 的话,我们可以在这里来建立一个连接实现联系发评论的人.代码如下:
if (!$url) {
$url = "#"
}else{
$stat = $url
$url = "mailto:" . $url . ""
}
最后,我们可以按行来显示数据,并且关闭循环,最终的显示代码如下:
echo("<p><b>$msgTitle</b>
$msgTxt<br>
<div align=right>$hr:$min $timetype | $mo/$da/$yr | $msgId, <a href='$url'>$SigName</a></div></p>")
}
<p><b>Message Title</b>
Text within the message, blah blah<br>
<div align=right>Hour:Minute AM/PM | Month/Day/Year | Message ID, <a href='mailto:test@test.com'>Name with email link</a></div></p>
表单处理的程序: COMMENTADD.PHP
首先我们设置一些变量,然后通过表单把变量获得的数据提交到后台数据库中,并且请记住用户名和密码。
$assume = $_POST['assume']
$posterEMAIL = $_POST['postemail']
$postTXT = $_POST['posttxt']
$posterNAME = $_POST['poster']
$postTITLE = $_POST['posttitle']
if ($assume == "true") {
$dbcnx = mysql_connect("localhost", "username", "password")
mysql_select_db("comments")
$sql = "INSERT INTO comtbl SET posterNAME='$posterNAME', posterEMAIL='$posterEMAIL',
postTXT='$postTXT', postTITLE='$postTITLE'"
if (mysql_query($sql)) {
echo("<P>Your comment has been added</P>")
} else {
echo("<P>Error adding entry: " . mysql_error() . "</P>")
}
}
提交了自己的评论之后还要有跳转的功能,下面的Javascript代码就可以实现跳转到指定的页面。
<script language=javascript>
<!--
location.href="comments.php"
//-->
</script>
下面是具体的COMMENTFORM.HTML代码,通过下面的内容,可以让发表评论者发表评论,然后通过提交可以把数据提交到commentadd.php里面来实现数据的在线提交。
<form action="commentadd.php" method=post>
<input type="text" name="poster" size="23" value="name"><br />
<input type="text" name="posttitle" size="23" value="name"><br />
<input type="text" name="postemail" size="23" value="user@email.com"><br />
<textarea cols=44 rows=6 name="posttxt" size=24 wrap="VIRTUAL">message<br />
<input type=hidden name=assume value=true>
<input type="submit" value="submit">
下面是处理评论的代码 comments.php:
<?
$dbcnx = mysql_connect("localhost", "username", "password")
mysql_select_db("comments")
$result = @mysql_query("SELECT * FROM comtbl ORDER BY postID DESC")
if (!$result) { echo("<b>Error performing query: " . mysql_error() . "</b>")
exit()
}
while ($row = mysql_fetch_array($result) ) {
$msgTxt = $row["postTXT"]
$msgId = $row["postID"]
$SigName = $row["posterNAME"]
$SigDate = $row["postTIME"]
$msgTitle = $row["postTITLE"]
$url = $row["posterEMAIL"]
$yr = substr($SigDate, 2, 2)
$mo = substr($SigDate, 4, 2)
$da = substr($SigDate, 6, 2)
$hr = substr($SigDate, 8, 2)
$min = substr($SigDate, 10, 2)
if ($hr >"11") {
$x = "12"
$timetype = "PM"
$hr = $hr - 12
}else{
$timetype = "AM"
}
if (!$url) {
$url = "#"
}else{
$stat = $url
$url = "mailto:" . $url . ""
}
echo("<p><b>$msgTitle</b>$msgTxt<br><div align=right>
$hr:$min $timetype | $mo/$da/$yr | $msgId, <a href='$url'>$SigName</a></div></p>")
}
?>
下面是 commentadd.php:
<?
$assume = $_POST['assume']
$posterEMAIL = $_POST['postemail']
$postTXT = $_POST['posttxt']
$posterNAME = $_POST['poster']
$postTITLE = $_POST['posttitle']
if ($assume == "true") {
$dbcnx = mysql_connect("localhost", "username", "password")
mysql_select_db("comments")
$sql = "INSERT INTO comtbl SET posterNAME='$posterNAME', posterEMAIL='$posterEMAIL',
postTXT='$postTXT', postTITLE='$postTITLE'"
if (mysql_query($sql)) {
echo("Your comment has been added")
} else {
echo("Error adding entry: " . mysql_error() . "")
}
}
?>
<script language=javascript>
<!--
location.href="comments.php"
//-->
</script>
HTML 代码留言板 一个超简陋的留言版 免费留言板 //评论内容不能保存 因为涉及到数据库<script>
function Ok_OnClick(event)
{
if(document.getElementsByName("textName")[0].value == ""){
alert("请输入内容")
return
}//getElementById
var table
var tableList = document.getElementsByTagName("TABLE")
for(var i = 0 i <tableList.length i++) {
if(tableList[i].name == "tableName") {
table = tableList[i]
break
}
}
var value = document.getElementsByName("textName")[0].value
var index = table.rows.length
table.insertRow(index)
table.rows(index).insertCell(0)
table.rows(index).cells(0).innerText = value
document.getElementsByName("textName")[0].value = ""
}
</script>
<HTML>
<HEAD>
<META http-equiv="Content-Type" content="text/htmlcharset=gb2312">
<TITLE>留言版</TITLE>
<STYLE>
td{width:20%height:20pxborder-bottom:1px solid blackborder-right:1px solid blackcursor:default}
div{font-size:13px}
th{height:20pxfont-size:12pxfont-weight:normalborder-bottom:2px solid blackbackground-color:#CCCCCC}
table{border:1px solid blackfont-size:13px}
</STYLE>
</HEAD>
<BODY oncontextmenu="return false"">
<TABLE name="tableName" width="70%" align="center" cellpadding="0" cellspacing="0">
<TR>
<TH colspan="4" style="">留言版</TH>
</TR>
<TR>
<TD>呵呵,一个超简陋的留言版 ^_^</TD>
</TR>
</TABLE>
<BR>
<div align="center">
<textarea rows="2" name="textName" id="textId" cols="20"
style="width: 70%clip: rect(0pt, 47pt, 18pt, 0pt)font-family: 宋体font-size: 10ptfont-weight: normalfont-style: normaltext-align: leftvertical-align: middletext-indent: 0color: #000000background-attachment: fixedbackground-color: #EEEEEEborder: 1px solid #0xf8f8f8">
</textarea>
</div>
<BR>
<div align="center">
<input type="button" value="提交" onclick="return Ok_OnClick(window.event)"
style="align:center width:60 height: 21clip: rect(0pt, 47pt, 18pt, 0pt)font-family: 宋体font-size: 10ptfont-weight: normalfont-style: normalcursor: handtext-align: centervertical-align: middletext-indent: 0color: #000000background-attachment: fixedbackground-color: #d8d8d8border: 1px solid #59b4f7">
<!--position: absoluteleft: 342top: 184-->
</div>
</BODY>
</HTML>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)