0

there. I am creating a blog website and I want to post some comments on that post. but the form is reloading am not getting data to store MongoDB. I search on the internet so I got ajax but ajax is not working and this is new for me. help me out with this problem. I am using node js and ejs templating and MongoDB. node js code

  app.post("/do-comment", async (req,res) =>{      
    const comment = new Comment({name:req.body.name,comment:req.body.comment});
    await comment.save();
    await Blog.findOneAndUpdate({_id:req.body._id}, {$push: {comment}});
    res.send("Comment was added successfully");
})  

HTML code

  <!DOCTYPE html>

<html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>Blogs</title>
        <meta name="description" content="">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
    </script>

       
        <!-- CSS only -->
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<!-- JavaScript Bundle with Popper -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
    </head>
    <body>
        <nav class="navbar navbar-expand-lg navbar-light bg-primary">
            <div class="container-fluid">
              <a class="navbar-brand" href="#">Blogs</a>
              <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarTogglerDemo02" aria-controls="navbarTogglerDemo02" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
              </button>
              <div class="collapse navbar-collapse" id="navbarTogglerDemo02">
                <ul class="navbar-nav me-auto mb-2 mb-lg-0">
                  <li class="nav-item">
                    <a class="nav-link active" aria-current="page" href="#">Home</a>
                  </li>
                  <li class="nav-item">
                    <a class="nav-link" href="/compose">compose</a>
                  </li>
                  <li class="nav-item">
                    <a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disabled</a>
                  </li>
                </ul>
                <form class="d-flex">
                  <input class="form-control me-2" type="search" placeholder="Search" aria-label="Search">
                  <button class="btn btn-outline-success" type="submit">Search</button>
                </form>
              </div>
            </div>
          </nav>

<div class="container">
    <div class="col">
        <img src="data:image/<%=post.image.contentType%>;base64,
        <%=post.image.data.toString('base64')%>" alt="photo"width="1000px" height="500px" alt="photu">
    <h1><%= post.title %></h1>
    <p><%= post.content %></p>
</div>
<div class=''>
    // form is here for comment
    <form method="POST" onsubmit="return doComment(this);" class="theme-form-one">
        <input type="hidden" name="_id" value="<%=post._id %>">
            <div class="row">
             <div class="col-md-6 col-12"><input type="text" name="name" placeholder="Name"></div>
             <div class="col-12"><textarea name="comment" placeholder="Comments"></textarea></div>
          </div>
        <button class="theme-button-one">POST COMMENT</button>
    </form>
</div>
//// catching comments here
<div class="">
    <div class="container-fluid">
       <% data.forEach(function(cData){ %>
            <h1><%= cData.name %></h1>
            <p><%= cData.comment %></p>
        <% }) %>
        
    </div>
</div>
</div>
//js code here
<script>
    function doComment(form){
    $.ajax({
    url:"/post/do-comment",
    method:"POST",
    data:{name:form.name.value,comment:form.comment.value,
        _id:form._id.value},
    success: function(response){
    alert(response);
        }
    });
    return false;
}
</script>
</body>

</html>
3
  • i have add my code here Commented Aug 4, 2021 at 7:27
  • To resolve your issue, please provide the community with more info, add your html and js code (it would be great,if you add 1 html, 2 ejs, 2 node js code blocks, it will be easier for us to help you. it reloads probably because you have <button type="submit">Name</button> try to add e.preventDefault() in the button click listener, at the very beginning and try to take a look here stackoverflow.com/questions/41369780/… Commented Aug 4, 2021 at 7:27
  • Please take a look here stackoverflow.com/questions/11588507/…, and replace your onsubmit="return doComment(this);" with just js/html code shown in the example link, if you still have questions,please let me know, I will try to compose it for you. Please, pay attention to each aspect, event.preventDefault(); but your ajax data and url are just fine, Commented Aug 4, 2021 at 7:41

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.