简体   繁体   English

从ejs html表获取ID并将其发送到node.js服务器

[英]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. 我的ejs文件中有此表,其中显示了用户的信息。 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. 当用户单击批准时,它将检索该特定用户的ID,并将该ID发送到服务器以更新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. 我的问题是我不确定如何获取用户特定行的ID并将ID数据发送到服务器。 I know how to do it with forms, but I am confused on the table. 我知道如何用表格来做,但是在桌子上我很困惑。 Any help will be appreciated. 任何帮助将不胜感激。

js file js文件

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 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: 如果您想发表multipart/form-datasubmit ,添加一个dom保存您的PARAMS像您的前端代码:

<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. 通常我们使用javascript控制包括http请求在内的所有内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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