这个答案适用于将来可能有类似问题的任何人。
尽管在没有数据库的情况下添加评论是不切实际的,但它是可行的。下面是如何去做。
第 1 步:创建一个文件并将其保存为 .php 扩展名,如 comment.php 第 2 步。创建通常的 html 表单并将表单方法 =“post”和表单操作设置为文件名,如 action =“comment.php”。 php"
<h3> Add a comment here </h3>
<form action="comment.php" method="post">
<label for="name">Name:</label>
<input type="text" name="yourname"><br>
<label for="name">Comment:</label>
<textarea name="comment" id="comment" cols="30" rows="10"></textarea>
<input type="submit" value="submit">
</form>
` 步骤 3. 在同一文件 comment.php 中编写一个 php 脚本来处理来自表单的数据。请记住将脚本包含在 php 标记中
<?php
$yourname = $_POST['yourname'];
$comment = $_POST['comment'];
// format the comment data into how you want it to be displayed on the page
$data = $yourname . "<br>" . $comment . "<br><br>";
//Open a text file for writing and save it in a variable of your chosen.
//Remember to use "a" not "w" to indicate write. Using 'w' will overwrite
// any existing item in the file whenever a new item is written to it.
$myfile = fopen("comment.txt", "a");
//write the formatted data into the opened file and close it
fwrite($myfile, $data);
fclose($myfile);
// Reopen the file for reading, echo the content and close the file
$myfile = fopen("comment.txt", "r");
echo fread($myfile,filesize("comment.txt"));
?>