Home > Net >  How to send a post request that the user clicked on 1 of the buttons, and tell the server which one
How to send a post request that the user clicked on 1 of the buttons, and tell the server which one

Time:10-14

The task is as follows. The user (Admin) checks the entries for correctness, and then clicks the accept or reject button. There are several such records (3, but I don't think it matters), each record has 2 such buttons. I tried to do something like

<form action="/admin" method="POST">
 <input type="button" name="button" value="accept" class="admin-buttons">
</form>
<form action="/admin" method="POST">
 <input type="button" name="button" value="decline" class="admin-buttons">
</form>

and then on server

app.post('/admin', checkAuthenticated, (req, res) => {
if (req.body.button.value == 'accept'){} // changes on db
}

But it didn't work, so here I am.

CodePudding user response:

Found the error The code below should work

app.post('/admin', checkAuthenticated, (req, res) => {
if (req.body.button == 'accept'){} // changes on db
}

req.body.button.value will give you undefined as its taking button to be an object

Next time console.log(req.body) for better debugging

And for your forms

<form action="/admin" method="POST">
<input type="text" name="button" value="accept" class="admin-buttons" hidden="">
 <input type="submit" name="button" value="accept" class="admin-buttons">
</form>
<form action="/admin" method="POST">
<input type="text" name="button" value="decline" class="admin-buttons" hidden="">
 <input type="submit" name="button" value="decline" class="admin-buttons">
</form>
  • Related