[英]EJS - Updating data in my database without reloading the page from EJS
I am creating something like the reddit comment section, someone can comment something and people can reply to that comment. 我正在创建类似reddit评论部分的内容,有人可以发表评论,然后人们可以回复该评论。
I am using Node, Express, MySQL, EJS. 我正在使用Node,Express,MySQL,EJS。
The problem: I don't know how to make the upvote/downvote part, I don't know how to make an onclick event which will tell my DB to upvote/downvote that comment by 1 without reloading the page. 问题:我不知道如何制作upvote / downvote部分,我也不知道如何创建onclick事件,该事件将告诉我的数据库以1的价格对upupte / downvote该注释,而无需重新加载页面。
Express code: 快递代码:
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: 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>
<% } %>
I tried trying something with Fetch, but it didn't work properly for me.(I don't know what to write as the URL) 我尝试使用Fetch尝试尝试一些操作,但对我来说却无法正常工作。(我不知道该写什么作为URL)
Cool. 凉。 So one of the neat things about express is that you can make whatever endpoints you want, that will do whatever you want.
因此,关于express的整洁事情之一是,您可以创建所需的任何端点,即可以执行所需的任何操作。 So let's say you want to make an AJAX request that upvotes a certain comment?
假设您要提出一个支持某条评论的AJAX请求? You can create it.
您可以创建它。
(These are PUT
routes because they change information about a record that already exists.) (这些是
PUT
路由,因为它们更改有关已存在记录的信息。)
router.put('/posts/upvote/:comment_id', (req, res) => {
// something that increments the number of upvotes for that particular comment
res.status(200).json({voteNum: // new vote number})
})
router.put('/posts/downvote/:comment_id, (req, res) => {
// something that decrements the number of downvotes for that particular comment
res.status(200).json({voteNum: // new vote number})
})
And then you can use it: 然后您可以使用它:
// when the upvote button is clicked
fetch(`/posts/upvote/${your_comment_id}`, { method: 'PUT', credentials: 'include' })
.then(res => res.json())
.then(jsonRes => {
// get the element with the number of votes
element.innerText = jsonRes.voteNum
})
Of course, this will need some work to fit it into your code. 当然,这需要一些工作才能使其适合您的代码。 But, overall, if you're ever not sure what to write for the URL... just remember, you can make it up.
但是,总的来说,如果您不确定要为该URL写什么...,请记住,可以进行弥补。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.