Enter data from form into mongodb using nodejs

I am having trouble to enter data in my database based on the option selected from the form Here is my form-

    <div class="input">
        <label for="fever">Fever</label>
        <input type="radio" id="fever" name="fever" value="0">No
        <input type="radio" id="fever" name="fever" value="1">Yes<br><br>
    </div>
    <div class="input">
        <label for="eyesight">Eyesight</label>
        <input type="radio" id="eyesight" name="eyesight" value="0">Long Sightedness
        <input type="radio" id="eyesight" name="eyesight" value="1">Short Sightedness<br><br>
    </div>

How can I know which option the user has selected and enter that in my database

2 answers

  • answered 2018-10-11 20:06 Code First

    May be you need to do something like this to get the selected value jQuery get value of selected radio button

  • answered 2018-10-11 20:20 Michael

    First you need to not use duplicate IDs for your inputs. Then you can figure out which option is selected using .checked

    For example:

    const el = document.getElementById('clickMe');
        
        el.onclick = function() {
        	if (document.getElementById('fever0').checked) {
            alert('No fever');
          }
          if (document.getElementById('fever1').checked) {
            alert('Yes fever');
          }
        	if (document.getElementById('eyesight0').checked) {
            alert('Long Sightedness');
          }
          if (document.getElementById('eyesight1').checked) {
            alert('Short Sightedness');
          }
        };
    <div class="input">
          <label for="fever">Fever</label>
          <input type="radio" id="fever0" name="fever" value="0">No
          <input type="radio" id="fever1" name="fever" value="1">Yes<br><br>
        </div>
        <div class="input">
          <label for="eyesight">Eyesight</label>
          <input type="radio" id="eyesight0" name="eyesight" value="0">Long Sightedness
          <input type="radio" id="eyesight1" name="eyesight" value="1">Short Sightedness<br><br>
        </div>
        <div id="clickMe">
          Click me!
        </div>