dimanche 6 août 2017

How to get values of checkbox in node js using ejs?

My Code:

Html File:

<form action="/form" method="post" enctype="multipart/form-data">
       Name: <input type="text" name="name" /><br>
       Email: <input type="email" name="email" /> <br>
       Age: <input type="number" name="age" /> <br>
       Address: <textarea name="address" rows="10" cols="15"> </textarea><br>
       Category: <select name="cat">
                   <option value="1">Php</option>
                   <option value="2">NodeJs</option>
                   <option value="3">jQuery</option>
                 </select><br>
       Gender: <input type="radio" name="gen" value="m"/> Male
               <input type="radio" name="gen" value="f"/> Female
       Hobby:   <input type="checkbox" name="hob[]" value="cri"/> cricket
                <input type="checkbox" name="hob[]" value="fot"/> football
                <input type="checkbox" name="hob[]" value="swi"/> swimming
   <input type="submit" value="Submit">
</form>  

Output Console in NodeJs:

{ name: 'My name',
  email: 'myname@xyz.co',
  age: '23',
  address: 'xyz loaacation pqr city ',
  cat: '1',
  gen: 'm',
  'hob[]': 'cri' }  

I am using formidable for file upload and it provides with fields and files options. Now all the above fields give me proper output when supplied data except for checkbox which i have no idea how or what to do to get output.

to get the above output i have just typed-> console.log(fields);
and you can access a property by ex:- fields.field_name. but it doesn't work for checkboxes.

what i want is to get the checkbox value to display to the user. checkbox can have multiple values selected.

Thank you.




Aucun commentaire:

Enregistrer un commentaire