0

I have a problem with my radio button value that doesn't get inserted into the database. I'm using java-script and firebase realtime database. Thank you for your answer.

function saveData() {
var fname = document.getElementById("fname");
var lname = document.getElementById("lname");
var selectedGender;
document.getElementsByName("gender").forEach(function(elm) {
  if (elm.checked) {
    selectedGender = elm.value;
  }
})
var bDay = document.getElementById("bDay");
var moNum = document.getElementById("moNum");
var emailAdd = document.getElementById("emailAdd");
var username = document.getElementById("username");
var password = document.getElementById("password");

insertData(fname.value, lname.value, selectedGender.value, bDay.value,
  moNum.value, emailAdd.value, username.value, password.value)
}


function insertData(fname,lname,selectedGender,bDay,moNum,emailAdd,username,password){
var firebaseRef = firebase.database().ref("users");
firebaseRef.push({
 Firstname: fname,
 Lastname: lname,
 Gender: selectedGender,
 Birthday: bDay,
 Mobile: moNum,
 Email: emailAdd,
 Username: username,
 Password: password
 

});
console.log("Insert Success");
signUp();
}
 Gender :  </td><td>
 <input type="radio" name="gender" id="gender" value="male" />Male
<input type="radio" name="gender" id="gender" value="female" />Female </td></tr> <br/>

1 Answers1

1

To get the value from a radio button you will need to loop over the radio button and determine which one is checked, as shown here: Get Radio Button Value with Javascript

Or for you case:

var selectedGender;
document.getElementsByName("gender").forEach(function(elm) {
  if (elm.checked) {
    selectedGender = elm.value;
  }
})
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Update your question with the code of what you tried please. In general the fastest way to get help is by [creating a so-called MCVE](http://stackoverflow.com/help/mcve), especially if you also provide that in a site like jsbin/jsfiddle where we can all look at the same thing. – Frank van Puffelen Jan 26 '18 at 15:46
  • Please update the **question**, don't post the edit as an answer (since those get reordered by Stack Overflow). But in the code you posted now, you didn't correctly copy the code from my answer: `var selectedGender;` is on its own line. – Frank van Puffelen Jan 26 '18 at 18:53
  • I really apologize. Anyway I try to edit follow you and no value insert to firebase. – Apologize Pam Jan 26 '18 at 19:04
  • Did you check what the value of `selectedGender = elm.value;` after the loop I gave? Keep in mind that Stack Overflow is a horribly inefficient interactive debugger, so the best way to help us help you is by stepping through the code in a debugger, seeing if the variables have the values you'd expect, and telling us where they don't. Lots of log statements may also help there. Or alternatively: reproduce the problem in a jsbin, so that we can be sure we're looking at the same thing. – Frank van Puffelen Jan 26 '18 at 22:08