Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know which button was pressed?

I'm learning the basics of node.js and express framework. I have a simple page with two buttons:

<form action="/home2" method="post">
    <button name="butt1">butt1</button>
    <button name="butt2">butt2</button>
</form>

And i want to see in console which button was pressed:

router.post('/', function(req, res, next) {
    console.log(req.body.name);
    res.render('home2', { title: 'post' });
});

In the console i just see

undefined

How can I access the name of the button?

like image 606
wiwo Avatar asked Nov 13 '15 23:11

wiwo


1 Answers

I think it would be helpful for you.

<form action="/home2" method="post">
   <button name="butt1">butt1</button>
   <button name="butt2">butt2</button>
</form>


router.post('/home2', function(req, res, next) {

  if(req.body.hasOwnProperty("butt1")){
     console.log("butt1 clicked");
  }else{
     console.log("butt2 clicked");
  }
  res.render('home2', { title: 'post' });
});
like image 84
Isabek Tashiev Avatar answered Oct 23 '22 04:10

Isabek Tashiev