Add href to a button in Pug without breaking a form

Issue

I’m trying to make my button href to my /leaderboard page after my quiz app submits.

    h1 Play Time 4 Trivia!
    button Go
    
    form(method='POST')
        ul
            each val in questions
                li
                    div
                        label= val.Question
                            select(name=val.QuestionId)
                                option(value='true') True
                                option(value='false') False

        button(type="submit") Submit      

how would I go about having my submit button also redirect to another page without breaking my form submit?

Solution

You shouldn’t be doing this on the client side.

Say your form has action="/trivia", then at your Express server, assuming router is your Express app or a Router object:

router.post("/trivia", (req, res) => {
  // process the answers submitted in req.body
  // then when done:
  res.redirect("/leaderboard");
});

The above also has the advantage that if the user refreshes the page, the form is not submitted another time.

Answered By – Old Geezer

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published