Tuesday 20 March 2018

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.I am using 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.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: //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++){ %>

<%= result[i].post %>

<% } %>I tried trying something with Fetch, but it didn't work properly for me.(I don't know what to write as the URL)

Submitted March 20, 2018 at 10:49AM by khazha88

No comments:

Post a Comment