如何制作评论框,最好使用 javascript 或 php,然后显示评论?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19203978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I make a comment box, using preferablely javascript or php, and then show the comments?
提问by user2850689
I am trying to make a comment box for my website, I can make the background and other items using html and css, but I want people to be able to leave comment, questions, or concerns, to improve the users enjoyment. I have researched many ways of how I could possiblely make a comment box, how to write it out to a file, how to show the comments, and how to update the file, but because I personally don't know PHP or Java Scripting I don't know how to do any of that. I have looked at other peoples coding and have managed to come up with something along the lines of this:
我正在尝试为我的网站制作一个评论框,我可以使用 html 和 css 制作背景和其他项目,但我希望人们能够发表评论、问题或疑虑,以提高用户的享受。我已经研究了很多方法来如何制作评论框、如何将其写入文件、如何显示评论以及如何更新文件,但是因为我个人不了解 PHP 或 Java 脚本,所以我不知道该怎么做。我看过其他人的编码,并设法想出了一些类似的东西:
This is for the form (It is in an html):
这是用于表单(在 html 中):
<div class="commentf">
<table>
<tbody>
<FORM action="submit.html" method="post">
<tr>
<td><LABEL for="name">Name: </LABEL>
<INPUT type="text" id="name"></td>
</tr>
<tr>
<td><LABEL for="email">E-Mail: </LABEL>
<INPUT type="text" id="email"></td>
</tr>
<tr>
<td><LABEL for="subject">Subject: </LABEL>
<INPUT type="text" id="subject"></td>
</tr>
<tr>
<td><LABEL for="comment">Text: </LABEL>
<TEXTAREA type="text" id="comment">Comment:</TEXTAREA></td>
</tr>
<tr>
<td><INPUT type="submit" value="Submit"> <INPUT type="reset"></td>
</tr>
</FORM>
</tbody>
</table>
</div>
And this is the PHP file (saved as an html, for some reason when I try to open it as a php file it opens a save as box instead of running the php, so I just saved it as a html) that "processes" the information:
这是“处理”的 PHP 文件(保存为 html,出于某种原因,当我尝试将其作为 php 文件打开时,它会打开一个另存为框而不是运行 php,所以我只是将其另存为 html)信息:
<?php
if(isset($_POST['name']) && isset($_POST['email'] && isset ($_POST['subject'] && isset ($_POST['comment'])))) {
$data = $_POST['name'] . '-' . $_POST['email'] . '-' . $_POST['subject'] . '-' . $_POST['comment'] . "\n";
$ret = file_put_contents('HAS.txt', $data, FILE_APPEND | LOCK_EX);
if($ret === false) {
die('There was an error writing this file');
}
else {
echo "$ret bytes written to file";
}
}
else {
die('no post data to process');
}
Lastly, this is part of the html that I first displayed, so that it would show the comments...
最后,这是我第一次显示的 html 的一部分,以便它显示评论......
<div class="postcomment">
<FORM>
<br>Name:</b> <?php echo $_POST['name']; ?> <INPUT type="text" id="name">
<br>E-Mail:</b> <?php echo $_POST['email']; ?> <INPUT type="text" id="email">
<br>Subject:</b> <?php echo $_POST['subject']; ?> <INPUT type="text" id="subject">
<br>Comment:</b> <?php echo $_POST['comment']; ?> <TEXTAREA type="text" id="comment"></TEXTAREA>
</FORM>
</div>
I know it is a big mess and all... but I'd appreciate it a lot if any one would respond to me with the correct answer. If any one, for whatever reason doesn't understand the question, I'd be more than happy to collaborate with them to better understand the question to progress further into my website.
我知道这是一团糟……但如果有人能以正确的答案回应我,我将不胜感激。如果任何人,无论出于何种原因不理解该问题,我都非常乐意与他们合作以更好地理解该问题,从而进一步进入我的网站。
Thanks!!!
谢谢!!!
回答by michael spencer
If you haven't found an answer yet heres a simple html way of creating comments section for your website it contains the php
如果您还没有找到答案,这里有一个简单的 html 方式为您的网站创建评论部分,它包含 php
<?php
if ($_POST){
$name = $_POST['name'];
$content = $_POST['commentContent'];
$handle = fopen("comments.html","a");
fwrite ($handle,"<b>" . $name . "</b></br>" . $content . "</br>");
fclose ($handle);}
?>
<html>
<body>
<form action="" method="POST">
Content: <textarea rows ="10" cols ="30" name="commentContent"></textarea></br>
Name: <input type = "text" name = "name"></br>
<input type = "submit" value = "post!"></br>
</form>
<?php include "comments.html"; ?>
</body>
</html>
just create a blank html called comments.html in same folder hope some help if no answer yet
只需在同一文件夹中创建一个名为 comments.html 的空白 html 如果还没有答案,希望能有所帮助
回答by jophab
Currently you are using the id of the input, textarea tags to access it values from $_POST. That is not possible. You have to use the name attribute of the tag to access it value from $_POST.
当前,您正在使用输入的 id、textarea 标签来访问 $_POST 中的值。这是不可能的。您必须使用标签的 name 属性从 $_POST 访问它的值。
<div class="postcomment">
<FORM>
<br>Name:</b> <?php echo $_POST['name']; ?> <INPUT type="text" name="name">
<br>E-Mail:</b> <?php echo $_POST['email']; ?> <INPUT type="text" name="email">
<br>Subject:</b> <?php echo $_POST['subject']; ?> <INPUT type="text" name="subject">
<br>Comment:</b> <?php echo $_POST['comment']; ?> <TEXTAREA type="text" name="comment"></TEXTAREA>
</FORM>