显然,我是一名新手网页设计师.我正在使用php和sql做所有引擎盖下的东西,但我想要一个视觉上吸引人的功能评论系统.
现在我只是使用HTML表单,但它们看起来不太好.我应该使用javascript吗?让我入门的任何提示?
如果你只是想要评论而不是寻找学习经验,那么切一些角落并使用Disqus!这很容易设置.
现在我只是使用HTML表单,但它们看起来不太好.我应该使用javascript吗?
如果您使用JavaScript,那么无论如何都会在那里的某个地方放置HTML表单.
基本上你想要:
comments<form>要在其中评论的所有页面底部的HTML ,其中的action属性指向您的"comment.php"脚本.comments使用以下列在数据库中创建表:
一点点SQL:
CREATE TABLE `comments`
( `id` INT NOT NULL AUTO_INCREMENT,
`url` VARCHAR(255) NOT NULL ,
`author` VARCHAR(255) NOT NULL ,
`comment` TEXT NOT NULL ,
PRIMARY KEY (id)
)
Run Code Online (Sandbox Code Playgroud)
没有承诺我的语法是完美的;)
让它响应POST请求并在表中插入一行comments.(为什么POST?阅读安全方法)
<?php
$db = DB::connect(...);
// I'm being a little lazy with the intricacies of PHP-DB interaction...
// Be sure to validate the input a little.
// You'll want to take some measures to prevent spoofing.
$db->insert("INSERT INTO `comments` (`url`, `author`,`comment`) VALUES (?,?,?)", array($_POST['url'], $_POST['author'], $_POST['comment']));
header("Location: ".$_POST['url']); // redirect back to the commented page
// actually, I'm being lazy;the Location header is supposed to be a full url including protocol and domain etc
?>
Run Code Online (Sandbox Code Playgroud)
请注意,我?在SQL中使用了占位符 ...
在包含评论的所有页面的底部,添加类似...的内容
<?php
$db = DB::connect(...);
// I'm being a little lazy with the intricacies of PHP-DB interaction...
$result = $db->query("SELECT * FROM `comments` WHERE `url`=?", array($_SERVER['REQUEST_URI']));
while($row = $result->fetchRow()) }
echo "<div class=\"comment\">";
echo "<div class=\"author\">Comment By:".htmlentities($row['author'])."</div>";
echo htmlentities($row['comment']);
echo "</div>";
}
?>
Run Code Online (Sandbox Code Playgroud)
注意使用htmlentities防止XSS.
<form>在所有页面的底部添加HTML现在,要让用户能够添加注释,您需要一个HTML <form>.
<form method="post" action="/path/to/comments.php">
<label for="author">Your name:</label><input name="author" id="author" />
<textarea name="comment"></textarea>
<input type="hidden" name="url" value="<?php echo htmlentities($_SERVER['REQUEST_URI'])?>" />
<input type="submit" />
</form>
Run Code Online (Sandbox Code Playgroud)
但是,这样做,你只有文字(没有很酷的引力或任何东西),你真的只是信任他们的名字(没有OpenID认证).你也没有真正的评论格式(它们都是纯文本).
这一切都通过使用Disqus来解决.
| 归档时间: |
|
| 查看次数: |
13153 次 |
| 最近记录: |