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.
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") {
...
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) {
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With