dimanche 6 juin 2021

How to store and retrieve checkbox state on refresh of page? Mongoose/EJS/Nodejs/Express

I have a checkbox, essentially like a to do list. When i check the box, I want to retain the state even after i refresh the page by using mongoose. I do know of localstorage, however EJS runs on a server side. Currently I am able to post the checkbox to my express side.

IN EJS

form id="form2" action="/check" method="post"></form>
<input form="form2" type="checkbox" name="checkboxx" onChange="this.form.submit()">

IN EXPRESS

app.post("/check", function(req,res){
  Item.updateOne({checked: req.body.checkboxx.checked}, function(err){
    if(err){
      console.log(err);
    } else console.log("successfully updated db")
  })
  
  res.redirect("/")
})

My collection is "Item", the schema includes a boolean property "checked" checkboxx is the name of the input checkbox. Any way to update the information of the checkbox state and reload the saved states upon refreshing the page?




Aucun commentaire:

Enregistrer un commentaire