我的ejs文件中有这个表,显示了用户的信息。每个用户旁边都有两个按钮,一个用于批准,另一个用于拒绝。当用户点击批准时,它会检索该特定用户的ID并将该ID发送到服务器以更新mongodb。与拒绝按钮相同。我的问题是我不知道如何为用户检索特定行的id并将id数据发送到服务器。我知道如何用表格来做,但我在桌子上感到困惑。任何帮助将不胜感激。
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>
答案 0 :(得分:0)
如果您想使用multipart/form-data
发布submit
,请添加dom
以将您的参数保存在前端代码中,例如:
<hidden name="approveId" value=''>
并且您的服务器获得了如下的参数:
var id = req.body.approveId;
通常我们使用javascript来控制包含http请求的所有内容。