【问题标题】:Get Id from ejs html table and send it to node.js server从 ejs html 表中获取 Id 并将其发送到 node.js 服务器
【发布时间】:2018-01-29 11:20:17
【问题描述】:

我的 ejs 文件中有这张表,它显示了用户的信息。每个用户旁边都有两个按钮,一个用于批准,一个用于拒绝。当用户单击批准时,它会检索该特定用户的 id 并将 id 发送到服务器以更新 mongodb。与拒绝按钮相同。我的问题是我不确定如何为用户检索特定行的 id 并将 id 数据发送到服务器。我知道如何使用表格来做到这一点,但我在桌子上感到困惑。任何帮助将不胜感激。

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

<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>

【问题讨论】:

    标签: javascript node.js mongodb ejs


    【解决方案1】:

    如果您想使用submit 发布multipart/form-data,请添加dom 以将您的参数保存在前端代码中,例如:

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

    你的服务器会得到如下参数:

    var id = req.body.approveId;
    

    通常我们使用javascript来控制所有的东西,包括http请求。

    【讨论】:

      猜你喜欢
      • 2018-11-08
      • 1970-01-01
      • 2012-12-03
      • 2022-01-27
      • 2014-06-16
      • 1970-01-01
      • 1970-01-01
      • 2013-10-17
      • 2018-11-10
      相关资源
      最近更新 更多