提问人:veeger9 提问时间:6/9/2023 更新时间:6/10/2023 访问量:107
如何在不刷新页面的情况下在同一页面中显示新发布的评论 [duplicate]
How to get new posted comment to show in same page without page refresh [duplicate]
问:
一个相当复杂的场景。博客脚本post.php,有一个表单,用于向文章标题提交评论,该文章标题被单击并显示给用户。这是脚本的表单部分:
$id = (int) $_GET['id']; //at beginning of script - obtained from title clicked on by user
<form action="post.php?id=<?php echo $id; ?>" method="post">
<?php
if ($logged == 'No') {
$guest = 'Yes';
?>
<label for="name"><i class="fa fa-user"></i> Your Name:</label>
<input type="text" name="author" value="" class="form-control" required />
<br />
<?php
}
?>
<label for="input-message"><i class="fa fa-comment"></i> Comment: (255 character limit including spaces and punctuation)</label>
<textarea id="mylmnt" maxlength="255" name="message" rows="5" class="form-control" required></textarea>
<b><span id="mylmntLeft"></span></b>
<br />
这是脚本的 db 插入部分:
$runq = mysqli_query($connect, "INSERT INTO `comments` (`post_id`, `comment`, `user_id`, `date`, `time`, `guest`) VALUES ('$row[id]', '$comment', '$author', '$date', '$time', '$guest')");
echo '<div class="alert alert-success">Your comment has been successfully posted</div>';
之后是我的html模板,用于向订阅者发送电子邮件,说明已发布新评论。这一切都很好。在我的模板之后,有一行:
}
echo '<meta http-equiv="refresh" content="0;url=post.php?id=' . $row['id'] . '#comments">';
这将刷新post.php页面并向下跳转到 #comment 部分。我的问题是刚刚发布的新评论没有显示。到目前为止,看到它的唯一方法是点击浏览器重新加载按钮。我知道要完成我需要的东西,我必须使用 ajax 和 jquery,但我不知道该怎么做以及事情应该去哪里。我已经阅读并研究了许多教程和代码发布,但这些都没有帮助我开始使用解决方案。
我试过window.location.reload();和 window.location.reload(true);这没有用。正如我已经说过的,我不知道如何使用ajax和jquery来解决这个问题。
答:
0赞
veeger9
6/10/2023
#1
好吧,与我密切合作的朋友也运行这个博客软件,已经找到了解决我/我们问题的方法。但首先我要说的是,我从未展示过的核心文件提供了所有的输入环境,因此脚本实际上非常安全。发布问题的答案是这样的。 现在,新评论显示在“提交”之后。感谢所有试图提供帮助的人。<form action="post.php?id=<?php echo $id, $_SERVER['PHP_SELF']; ?>" method="post">
评论
标题
,您不需要 Javascript。