在未重新加载页面的发布后显示数据

发布于 2025-01-28 15:56:08 字数 2201 浏览 1 评论 0原文

我有这个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文