POST 使用 file_put_contents 发表多条评论
POST'ing multiple comments using file_put_contents
好的,这是我的页面(为练习重新创建的基本模板):
http://puu.sh/fRn4b/6d83015087.png
它都是静态的,禁止表格。
我试图用上述表格做的是让输入的内容显示在下面的小灰色框中(它设置了溢出,所以一旦填满就会启用滚动)。
我从这个开始:
<form action="index.php" method="post">
<input type="text" name="name" placeholder="Enter your name" required><br>
<textarea name="comment" id="comments" rows="10" required placeholder="Enter your thoughts"></textarea><br>
<input type="submit" name="submit">
</form>
if (isset($_POST['name']) && isset($_POST['comment'])) {
$name = htmlentities($_POST['name']);
$comment = htmlentities($_POST['comment']);
$fullcomment = "<h2>".$name."</h2><p>".$comment."</p>";
echo $fullcomment;
}
这很有效,而且 css 看起来还不错。但是,它只会 post 一条评论,并且该评论将在重新加载时丢失。我想让它坚持下去。那么接下来的尝试是:
if (isset($_POST['name']) && isset($_POST['comment'])) {
$file ="./index.php";
$name = htmlentities($_POST['name']);
$comment = htmlentities($_POST['comment']);
$fullcomment = "<h2>".$name."</h2><p>".$comment."</p>";
file_put_contents($file, $fullcomment, FILE_APPEND);
}
现在提交表单后,什么也不会发生(包括没有错误)。不能在文件中使用 file_put_contents 说函数(?)在里面吗?因为我尝试将 $file 更改为“./index.txt”并且成功了,所以它创建了一个新文件并在其中添加了表单内容。
你可能会说我对此非常陌生。这是我在学习。我学习新事物并思考应用它们的方法,即使这些方法不是最有效的方法。
任何帮助都将非常感谢!
它确实有效,但它在文档末尾添加了内容,而不是在评论框中。一定要另找方法。
编辑 2:我想我可以从 index.txt 疑难解答中获取内容并将其添加到 .php 文件,但这似乎有点迂回。
编辑 3:行得通。虽然现在刷新index.php会重复上次输入的表单内容,比较烦人。解决方案是在代码具有 运行 之后重置变量吗?
你可以使用Ajax来解决这个问题,但如果你不熟悉它,你可以试试这个:
$file ="./comments.txt";
// When you the page is loaded, get the comments from 'comments.txt'
$fullcomment = file_get_contents($file);
if (isset($_POST['name']) && isset($_POST['comment'])) {
$name = htmlentities($_POST['name']);
$comment = htmlentities($_POST['comment']);
$fullcomment = "<h2>".$name."</h2><p>".$comment."</p>";
file_put_contents($file, $fullcomment, FILE_APPEND);
}
好的,这是我的页面(为练习重新创建的基本模板): http://puu.sh/fRn4b/6d83015087.png
它都是静态的,禁止表格。
我试图用上述表格做的是让输入的内容显示在下面的小灰色框中(它设置了溢出,所以一旦填满就会启用滚动)。
我从这个开始:
<form action="index.php" method="post">
<input type="text" name="name" placeholder="Enter your name" required><br>
<textarea name="comment" id="comments" rows="10" required placeholder="Enter your thoughts"></textarea><br>
<input type="submit" name="submit">
</form>
if (isset($_POST['name']) && isset($_POST['comment'])) {
$name = htmlentities($_POST['name']);
$comment = htmlentities($_POST['comment']);
$fullcomment = "<h2>".$name."</h2><p>".$comment."</p>";
echo $fullcomment;
}
这很有效,而且 css 看起来还不错。但是,它只会 post 一条评论,并且该评论将在重新加载时丢失。我想让它坚持下去。那么接下来的尝试是:
if (isset($_POST['name']) && isset($_POST['comment'])) {
$file ="./index.php";
$name = htmlentities($_POST['name']);
$comment = htmlentities($_POST['comment']);
$fullcomment = "<h2>".$name."</h2><p>".$comment."</p>";
file_put_contents($file, $fullcomment, FILE_APPEND);
}
现在提交表单后,什么也不会发生(包括没有错误)。不能在文件中使用 file_put_contents 说函数(?)在里面吗?因为我尝试将 $file 更改为“./index.txt”并且成功了,所以它创建了一个新文件并在其中添加了表单内容。
你可能会说我对此非常陌生。这是我在学习。我学习新事物并思考应用它们的方法,即使这些方法不是最有效的方法。
任何帮助都将非常感谢! 它确实有效,但它在文档末尾添加了内容,而不是在评论框中。一定要另找方法。
编辑 2:我想我可以从 index.txt 疑难解答中获取内容并将其添加到 .php 文件,但这似乎有点迂回。
编辑 3:行得通。虽然现在刷新index.php会重复上次输入的表单内容,比较烦人。解决方案是在代码具有 运行 之后重置变量吗?
你可以使用Ajax来解决这个问题,但如果你不熟悉它,你可以试试这个:
$file ="./comments.txt";
// When you the page is loaded, get the comments from 'comments.txt'
$fullcomment = file_get_contents($file);
if (isset($_POST['name']) && isset($_POST['comment'])) {
$name = htmlentities($_POST['name']);
$comment = htmlentities($_POST['comment']);
$fullcomment = "<h2>".$name."</h2><p>".$comment."</p>";
file_put_contents($file, $fullcomment, FILE_APPEND);
}