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 Answer 1

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;
  }
})
Sign up to request clarification or add additional context in comments.

4 Comments

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, especially if you also provide that in a site like jsbin/jsfiddle where we can all look at the same thing.
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.
I really apologize. Anyway I try to edit follow you and no value insert to firebase.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.