0

I read through a similiar post but the individual was using perl and something elss so it didn't help me. My question is how can i submit a form with node js/pug but remain on the same page.

In the pug form the method is set to POST and action set to /profile in my nodejs i'm using

    router.post("/profile", req, res, next){
        return res.redirect("back")
     }

The problem is that all this does is reload the page. I want to stay on the page and simple show a message saying "profile update".

Ari Jae
  • 5
  • 5

1 Answers1

0

Then don't return a redirect. Simply take the post data and process it, you can return the state of the operation, for example. Then however you should use an AJAX query instead of stock form, which will always redirect you to the action URL.

router.post("/profile", req, res, next){
    return updateProfile(req.body); // true or false
 }

And in jQuery, for example, you can perform an AJAX request

$.ajax({
  type: "POST",
  url: "/profile",
  data: profile_form_data_object,
  success: function(data) {
    alert("Result of the profile update was: " + data); 
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
vfioox
  • 730
  • 1
  • 8
  • 22