简体   繁体   中英

Get Id from ejs html table and send it to node.js server

I have this table in my ejs file that shows a user's information. Next to each user, there are two buttons, one for approve and one for deny. When the user clicks on approve, it retrieves the id for that specific user and sends the id to the server to update mongodb. Same as for deny button. My problem is I am not sure how I can retrieve an id for a specific row for a user and send the id data to the server. I know how to do it with forms, but I am confused on the table. Any help will be 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

<form method="post" action="/admin/viewusers">
    <table class="table table-bordered" name="tableName">
        <label>Show Table By:</label>
        <select name="viewTableBy" id="viewTableBy">
            <option value="all">All</option>
            <option value="recent">Recent</option>
            <option value="search_name">Search By Name</option>
            <option value="username">Search By Username</option>
        </select>
        <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Email</th>
            <th>Username</th>
            <th>Verified</th>
            <th></th>
        </tr>
        <% for(var i=0; i<result.length; i++) {%>
        <tr>
            <td><%= result[i]._id %></td>
            <td><%= result[i].name %></td>
            <td><%= result[i].email %></td>
            <td><%= result[i].username %></td>
            <td><%= result[i].verification %></td>
            <td><button type="submit" name="approve" formaction='/admin/approve'>Approve
            </button><button type="submit" name="deny" formaction='/admin/deny'>Deny</button></td>
        <% } %>
        </tr>
        <input type="text" name="text_input" id="text_input"> 
        <button type="submit" class="btn btn-default">Submit</button>
    </table>
</form>

if you want to post multipart/form-data with submit , add a dom to save your params in your front-end code like:

<hidden name="approveId" value=''>

and your server get the params like:

var id = req.body.approveId;

Usually we use javascript to controll all the thing include http request.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM