在未重新加载页面的发布后显示数据
我有这个node.js应用程序,您可以在其中发布有关教师个人资料的评论。
contert.ejs
<!-- Form -->
<div class="">
<form id="commentsForm" action="/teachers/<%=teacher[0].name%>" method="POST">
<textarea class="comments" placeholder="Write here..." name="comm" id="comment" required></textarea>
<button class="commentsButton" type="submit">Send comment</button>
</form>
</div>
<!-- Displaying comments from database with GET request -->
<div id="commentsDiv" class="commentsDB">
<% for(var i=0;i<comment.length;i++) { %>
<div>
<h5><%=comment[i].content%></h5>
...
</div>
<% } %>
</div>
index.js
// GET request (works fine)
router.get('/teachers/:name', function(req, res){
db.query("SELECT * FROM comments", function(err, result){
//...
res.render('teacher', {comments: result});
});
});
// POST request
router.post('/teachers/:name', (req, res) => {
const {comm} = req.body;
db.query("INSERT INTO comments (comment) VALUES (?)", [comm], function(err, post){
// Insert correctly on DB
});
});
在数据库上插入了TextArea上键入的数据,但我的网页试图重新加载并永远加载。
我想要的是在不重新加载页面的情况下显示插入的新数据,该怎么做?
我尝试直接使用contercy.ejs上的脚本插入新数据,但是它没有用:
<!-- removing: action="/teachers/<%=teacher[0].name%>" method="POST" from <form>
and adding: onClick="addComment() on <button>-->
<form id="commentsForm" >
<textarea class="comments" placeholder="Write here..." name="comm" id="comment" required></textarea>
<button class="commentsButton" type="submit" onClick="addComment()">Send comment</button>
</form>
<script>
function addComment(){
var input = $('#comment').val();
var newComment = document.createElement("h5");
var textComment = document.createTextNode(input);
newComment.appendChild(textComment);
document.getElementById().appendChild(newComment);
}
</script>
最好的方法是什么?
I have this node.js app where you can post comments on teachers profiles.
teacher.ejs
<!-- Form -->
<div class="">
<form id="commentsForm" action="/teachers/<%=teacher[0].name%>" method="POST">
<textarea class="comments" placeholder="Write here..." name="comm" id="comment" required></textarea>
<button class="commentsButton" type="submit">Send comment</button>
</form>
</div>
<!-- Displaying comments from database with GET request -->
<div id="commentsDiv" class="commentsDB">
<% for(var i=0;i<comment.length;i++) { %>
<div>
<h5><%=comment[i].content%></h5>
...
</div>
<% } %>
</div>
index.js
// GET request (works fine)
router.get('/teachers/:name', function(req, res){
db.query("SELECT * FROM comments", function(err, result){
//...
res.render('teacher', {comments: result});
});
});
// POST request
router.post('/teachers/:name', (req, res) => {
const {comm} = req.body;
db.query("INSERT INTO comments (comment) VALUES (?)", [comm], function(err, post){
// Insert correctly on DB
});
});
The data typed on the textarea is inserted on the database but my webpage tries to reload and stays loading forever.
What I want is to show the new data inserted without reloading the page, how can I do that?
I tried inserting the new data directly with a script on teacher.ejs but it didn't work:
<!-- removing: action="/teachers/<%=teacher[0].name%>" method="POST" from <form>
and adding: onClick="addComment() on <button>-->
<form id="commentsForm" >
<textarea class="comments" placeholder="Write here..." name="comm" id="comment" required></textarea>
<button class="commentsButton" type="submit" onClick="addComment()">Send comment</button>
</form>
<script>
function addComment(){
var input = $('#comment').val();
var newComment = document.createElement("h5");
var textComment = document.createTextNode(input);
newComment.appendChild(textComment);
document.getElementById().appendChild(newComment);
}
</script>
What is the best way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论