javascript - how to use a variable in req.body - nodejs - Stack Overflow

what I want to do is to get data from a formbut the form is being generated dynamically and so I wont

what I want to do is to get data from a form but the form is being generated dynamically and so I wont know how many rows will e out. so for the form I have (using ejs)

<form action="/attendance-data" method="post">
     <% var i=0 %>
<% rows.forEach(function(item){ %>
       <% i=i+1 %>
     <tr>
          <td><%= i+"." %></td>
        <td id="uan<%= i %>"> <%= item.uan  %> </td>
        <td><%= item.name %></td>
        <td><%= item.designation %></td>
        <td><input type="text" name="attendance<%= i %>" ></td>
     </tr>
    <% });   %>
     <input type="submit" value="submit" />
</form>

here i changes from 1 to 10 or whatever the length of the data is. so, example the form name goes from attendance1, attendance2, attendance3,.....

in the server side i am using

app.post('/attendance-data',function(req,res){
    var  daysPresent;
    var attendance;
    for(var i=1;i<=numberOfEmployees;i++){

        attendance = "attendance"+i;
        daysPresent = req.body.attendance;
        console.log('attendance is :',attendance);
        console.log('days present is :',daysPresent);
    }

});

here I want to get the value of the days present from the form for each loop. so, first loop , it gets req.body.attendance1 second it gets req.body.attendance2 , and so on but when I use the variable attendance instead of attendance1 and attendance2 directly, then i get undefined as a result.

I am only getting a result when I directly use req.body.attendance1 or req.body.attendance2 or something similar.

why is this happening? why cant I use the attendance variable which gets all the values from attendance1 , attendance2, ......

what I want to do is to get data from a form but the form is being generated dynamically and so I wont know how many rows will e out. so for the form I have (using ejs)

<form action="/attendance-data" method="post">
     <% var i=0 %>
<% rows.forEach(function(item){ %>
       <% i=i+1 %>
     <tr>
          <td><%= i+"." %></td>
        <td id="uan<%= i %>"> <%= item.uan  %> </td>
        <td><%= item.name %></td>
        <td><%= item.designation %></td>
        <td><input type="text" name="attendance<%= i %>" ></td>
     </tr>
    <% });   %>
     <input type="submit" value="submit" />
</form>

here i changes from 1 to 10 or whatever the length of the data is. so, example the form name goes from attendance1, attendance2, attendance3,.....

in the server side i am using

app.post('/attendance-data',function(req,res){
    var  daysPresent;
    var attendance;
    for(var i=1;i<=numberOfEmployees;i++){

        attendance = "attendance"+i;
        daysPresent = req.body.attendance;
        console.log('attendance is :',attendance);
        console.log('days present is :',daysPresent);
    }

});

here I want to get the value of the days present from the form for each loop. so, first loop , it gets req.body.attendance1 second it gets req.body.attendance2 , and so on but when I use the variable attendance instead of attendance1 and attendance2 directly, then i get undefined as a result.

I am only getting a result when I directly use req.body.attendance1 or req.body.attendance2 or something similar.

why is this happening? why cant I use the attendance variable which gets all the values from attendance1 , attendance2, ......

Share Improve this question asked Jan 29, 2017 at 7:41 farazfaraz 2,73314 gold badges43 silver badges65 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

The line grabbing the value of daysPresent:

daysPresent = req.body.attendance; 

Should be:

daysPresent = req.body[attendance]

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744938127a4602151.html

相关推荐

  • javascript - how to use a variable in req.body - nodejs - Stack Overflow

    what I want to do is to get data from a formbut the form is being generated dynamically and so I wont

    1天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信