簡體   English   中英

從ejs html表獲取ID並將其發送到node.js服務器

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

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

如果您想發表multipart/form-datasubmit ,添加一個dom保存您的PARAMS像您的前端代碼:

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

並且您的服務器得到如下參數:

var id = req.body.approveId;

通常我們使用javascript控制包括http請求在內的所有內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM