Transferring ejs HTML table’s ID to Node.js server

The HTML, JS, and Node JS codes are listed below. My goal is to send the button and any assistance would be appreciated. In the users.js file, you can post with the addition of a method to save your parameters in the front-end code. Then, your server can receive the parameters. Typically, JavaScript is used to control everything, including HTTP requests.

Question:

In my ejs file, there is a table displaying a user’s information, with an approve and deny button next to each. Clicking on either button sends the user’s id to
update mongodb
on the server. However, I’m unsure of how to retrieve an id for a specific row and send it to the server, as I’m familiar with doing so using forms but not tables. Any assistance would be greatly appreciated.

js file

router.post('/viewusers', function(req, res) { 
    var viewTableBy = req.body.viewTableBy;
    if(viewTableBy == 'all') {
        console.log('All is Selected...');
        db.users.find(function(err, doc) {
            if(err) {
                console.log('Error finding users...');
                res.send(err);
            } 
            console.log('Displaying all users...');
            res.render('viewusers', {result: doc});
        });
    }
    else if(viewTableBy == 'recent') {
        console.log('Recent is Selected...');
        db.users.find().sort({_id:-1}).limit(10).toArray(function(err, docs) {
            if(err) {
            console.log('ERROR');
           }
           console.log(docs);
           res.render('viewusers', {result: docs});
        });
    }
    else if(viewTableBy == 'search_name') {
        console.log('Search By Name is Selected...');
        var name = req.body.text_input;
        console.log('name ' + name);
        // searching the db for name only
        db.users.find({name: name}).toArray(function(err, docs) {
           if(err) {
               console.log('ERROR');
               res.send(err);
           } 
           console.log(docs);
           res.render('viewusers', {result: docs});
        });
    }
    else if(viewTableBy == 'username') {
        console.log('Search By username...');
        var username = req.body.text_input;
        db.users.find({username: username}).toArray(function(err, docs) {
           if(err) {
               console.log('ERROR');
               res.send(err);
           } 
           console.log(docs);
           res.render('viewusers', {result: docs});
        });
    }   
});
router.post('/approve', function(req, res) {
    console.log('User Approve');
    // need to get the id or username to identify which user is going to be approve, update mongodb the verification to approve and display the table with the new data
    var id = req.body._id;
    console.log(id); // shows undefined
});
router.post('/deny', function(req, res) {
    console.log('User Deny');
});

users.js

<% for(var i=0; i <% } %>
ID Name Email Username Verified
<%= result[i]._id %> <%= result[i].name %> <%= result[i].email %> <%= result[i].username %> <%= result[i].verification %>



Solution:

To include both

multipart/form-data

and

submit

in your post, make sure to add a

dom

to store your parameters in the front-end code.


Your server receives the parameters in the following manner:

var id = req.body.approveId;

Typically, JavaScript is utilized to manage all aspects, including HTTP requests.

Frequently Asked Questions