0

I have started learning web development. I was using forms in express with ejs. when I click on submit button the entire page refreshes. Is there any way I can prevent the page refresh. I saw other answers over here. When I use type="button" the answer data typed in form is not being submitted.

Here tweet is a list passed from main js page. when the form is submitted I would be using body-parser to collect button value.

          <% tweet.forEach(item=>{ %>
            <form action="/home" method="post">
              <div class="tweets">
                <span> <%= item.name %></span><br>
                <span> <%= item.data %></span><br>
                <button type="submit"
                  name="like"
                  value="<%= item.name %>" 
                  <i class="fa fa-thumbs-up" aria-hidden="true"> </i>
                </button>
              </div>
            </form>
        <%  }) %>


Kindly help. Thank you

  • 1
    Note: You will have to submit your form via Javascript (not let the browser post the form) and your Javascript will have to explicitly prevent the default form submission as show in the [duplicate](https://stackoverflow.com/questions/19454310/stop-form-refreshing-page-on-submit). – jfriend00 Apr 18 '22 at 08:29
  • 1
    Other references: [How to prevent page from reloading after form submit - JQuery](https://stackoverflow.com/questions/45634088/how-to-prevent-page-from-reloading-after-form-submit-jquery), – jfriend00 Apr 18 '22 at 08:32

0 Answers0