-1

When I have click on checkbox then req.body.task return undefined.

<input type="checkbox" name="task" autocomplete="off" checked="" onchange="onToDochange({{id}})">

Perform on change function which return id of checked or unchecked checkbox.

function onToDochange(id) {
    // console.log(id);
    fetch('/checkbox', {
       method: 'POST',
       body: JSON.stringify({global: id})
     })
    .then(res=>res.json())
    .then(res => console.log(res));
};

Routing

app.post('/checkbox', (req, res) => {
    console.log(req.body.task);
});

Working with expressJS nodeJs template engine handlebars

Varinder Sohal
  • 1,142
  • 5
  • 24
  • 44
  • on the server what do you want to access the `task` or `id`? Since from the client you are just sending id and that is what will be available to you on the server. Try inspecting `req.body` on the server and what it really contains. – Raghav Garg Sep 20 '18 at 13:19
  • First of all, I want to access ID and i just implement loop on it. Every checkbox carry their own id and when I inspecting it returns empty {} braces. – Varinder Sohal Sep 20 '18 at 13:24

1 Answers1

2

The way you are sending id is not correct.

What you need to do is bind your onclick event handler function and with that event get id or value whatever you need in the event handler function. Also you are setting id to a key called global but you are accessing with req.body.task which is not correct.

Workig code is available in codesandbox

Check below code for better understanding of how to send id and access the id in router.

<input type="checkbox" name="task" autocomplete="off" checked="" onclick='onToDochange(this)' value="task" id="2">


function onToDochange(event) {
    // console.log(event.id);
    fetch('/checkbox', {
       method: 'POST',
       headers: {
         'Accept': 'application/json',
         'Content-Type': 'application/json'
       },
       body: JSON.stringify({"id": event.id})
     })
    .then(res=>res.json())
    .then(res => console.log(res));
};

Routing

app.post('/checkbox', (req, res) => {
    console.log(req.body.id);
});
Hemadri Dasari
  • 32,666
  • 37
  • 119
  • 162
  • still getting undefined from req.body.task. Actually i need to get the id in routing page. do you have any idea about this?? – Varinder Sohal Sep 20 '18 at 11:35
  • @VarinderSohal Hey I made a mistake. You can try with my updated code it should work now. Let me know if you still have issues – Hemadri Dasari Sep 20 '18 at 11:49
  • i`m working with javascript and event.target is for jQuery – Varinder Sohal Sep 20 '18 at 12:24
  • Ok have a look at this https://codesandbox.io/s/500r5q9ryn. It's working fine with only javascript and I am able to read the id – Hemadri Dasari Sep 20 '18 at 13:05
  • I didn't know that you are using only javascript. I updated my answer now. In code sandbox it's working fine. my updated answer also should work in your machine. If it's not working then you must be doing something wrong somewhere else – Hemadri Dasari Sep 20 '18 at 13:08
  • Hey!! do you have idea about it ?? https://stackoverflow.com/questions/52441069/update-part-of-html-page-without-reloading-the-page-using-expressjs-nodejs-handl – Varinder Sohal Sep 21 '18 at 10:43