[英]Request body in express.js where body is a value from database
I don't know how to build code to insert data from html body to database where body is a something what I select from database.我不知道如何构建代码以将数据从 html 主体插入数据库,其中主体是数据库中的 select。
EJS: EJS:
<% categories.forEach((cat, index) => { %>
<div class="form-check">
<input class="form-check-input" type="checkbox" value="T" name="<%= cat.id %>" id="<%= cat.id %>">
<label class="form-check-label" for="">
<%= cat.name %>
</label>
</div>
<% }) %>
and my js how shoudl it look?我的 js 应该怎么看? (??? - don't know what to write here to get checkbox id's)
(??? - 不知道在这里写什么来获取复选框 id)
if (req.body.???) {
let cat_query = "INSERT INTO `cat` (m_id, cat_id) VALUES ?";
let values = [
[result.insertId, '???']
];
db.query(cat_query, [values], (err, res2) => {if (err) throw err;})}
my output when I check some checkboxes:当我检查一些复选框时,我的 output :
{ '3': 'T',
'11': 'T',
'18': 'T',
topic: 'test',
messagetype: 'N',
content: '', }
It works when I use value and change it from 'T'
to <%= cat_id =>
and let my input name make constant.当我使用 value 并将其从
'T'
更改为<%= cat_id =>
并让我的输入名称保持不变时,它会起作用。
<input class="form-check-input" type="checkbox" value="<%= cat.id %>" name="cat" id="<%= cat.id %>">
if (req.body.cat) {
const { cat } = req.body
cat.forEach(cat_id => {
let cat_query = "INSERT INTO `centrala` (messages_id, cat_id) VALUES ?";
let values = [
[result.insertId, cat_id]
];
db.query(cat_query, [values], (err, res2) => {if (err) throw err;})
})
}
maybe it will be useful to someone也许对某人有用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.