【发布时间】:2018-08-29 03:22:35
【问题描述】:
我正在创建类似于 reddit 评论部分的内容,有人可以发表评论,人们可以回复该评论。
我正在使用 Node、Express、MySQL、EJS。
问题:我不知道如何制作赞成/反对票部分,我不知道如何制作一个 onclick 事件,该事件将告诉我的数据库在不重新加载页面的情况下将该评论赞成/反对票 1。
快递代码:
app.get("/posts", function(req,res){
connection.query(`SELECT post FROM testingposts`, function(error,result){
if(error){
console.log(`error 757`)
} else {
console.log(`works btw`)
res.render("posts", {result: result })
}
})
})
app.post("/posts", function(req,res){
let post = req.body.post;
console.log(`req.body`)
console.log(req.body.theValue)
connection.query(`INSERT INTO testingposts(post) VALUES(?)`, [req.body.post], function(error,result){
if(error){
console.log(`error 555`)
} else {
res.redirect("/posts")
}
})
})
EJS:
//This part basically creates the html elements for the comments.
//But I haven't added the upvote/downvote elements yet.
<% for(let i = 0; i < result.length; i++){ %>
<div value="<%= i %>">
<p><%= result[i].post %></p>
<input id="<%= i %>" type="submit" value="reply" onclick=bob(event) >
<input type="submit" value="report" >
</div>
<form method="post" action="posts" style="display:none">
<textarea name="replytextarea" id="" cols="30" rows="10"></textarea>
<input type="text" class="forValue" name="theValue" value="5" style="display: none">
<input type="submit" value="submit btw" onclick="setValue(event)">
</form>
<% } %>
我尝试使用 Fetch 尝试一些东西,但它对我来说无法正常工作。(我不知道 URL 应该写什么)
【问题讨论】:
标签: javascript node.js express ejs