Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Submit Buttons - html express node js

I'm trying to do a like/dislike system. So essentially, there are two buttons on the html side.

<form method="post" name="ratings">
    <input type="submit" name="vote" value="like">
    <input type="submit" name="vote" value="dislike">
</form>

However, I only know how to do a single function. How can I seperate it? I'm working in node js.

So, like the first line of code looks like this:

router.post('/test/*', function (req, res) {

Like what would the if statement look like in javascript? or do I have to change the html code and do something with onclick? Any response would be appreciated.

like image 979
indianhottie Avatar asked Mar 02 '16 02:03

indianhottie


2 Answers

I figured it out. You can get the name from req.body.vote and then use that value to do whatever.

var inputValue = req.body.vote;

if (inputValue == "like") {
    ...
like image 109
indianhottie Avatar answered Sep 30 '22 17:09

indianhottie


I suggest you to use ajax, somelike this..

Buttons:

 <button name="btnLike" value="like" onclick="like()">
 <button name="btnDislike" value="dislike" onclick="dislike()">

JS:

function like(){
    $.ajax({
        url: "http://localhost:3000/like",
        type: "POST",
        data: {like: 'yes'}, //send this to server
        success: function(returned) {
             console.log(returnet); // here can get the return of route
        },
        error: function() {

        }
    });
}

function dislike(){
    $.ajax({
        url: "http://localhost:3000/dislike",
        type: "POST",
        data: {dislike: 'yes'}, //send this to server
        success: function(returned) {
             console.log(returnet); // here can get the return of route
        },
        error: function() {

        }
    });
}

Routes:

router.post('/like', function (req, res) {
}

router.post('/dislike', function (req, res) {
}
like image 36
BrTkCa Avatar answered Sep 30 '22 17:09

BrTkCa