ShiHang Ng
ShiHang Ng

Reputation: 67

How can I be able to add data into firebase database

i want to add the checked checkboxes value into timesession field in the database but i have no idea how to code it. It is possible one field to store more than one data ? If possible store into the field then how do i retrieve it out ?

var firebaseRef = firebase.database().ref('facility');

function submit(){
//how do i call the checkboxes value in the ul
firebaseRef.push().set();

}
<label style="color: #f2f2f2">Time Session</label> <br/>
    	
	<ul>
   <li><label for="chk1"><input type="checkbox" name="chk1" id="chk1" >09:00-11:00</label></li>
   <li><label for="chk2"><input type="checkbox" name="chk2" id="chk2" >12:00-14:00</label></li>
   <li><label for="chk3" ><input type="checkbox" name="chk3" id="chk3" >15:00-17:00</label></li>
   <li><label for="chk4"><input type="checkbox" name="chk4" id="chk4">18:00-20:00</label></li>
   
  </ul>
 <br/>
 <button onclick="submit()">Submit</button>
 
 
  
  <script src="https://www.gstatic.com/firebasejs/4.10.0/firebase.js"></script>
<script>
  // Initialize Firebase
  var config = {
    apiKey: "AIzaSyCtZEZ3LR-cOsEJXxL5JxCxOLlGZAgqgzw",
    authDomain: "communitysys-83bda.firebaseapp.com",
    databaseURL: "https://communitysys-83bda.firebaseio.com",
    projectId: "communitysys-83bda",
    storageBucket: "communitysys-83bda.appspot.com",
    messagingSenderId: "283084495842"
  };
  firebase.initializeApp(config);
  console.log(firebase);
  
  
  
</script>

Upvotes: 0

Views: 1573

Answers (1)

Peter Haddad
Peter Haddad

Reputation: 80914

inside the checkbox, you need to add value that will represent the value of the checkbox, like this:

<input type="checkbox" class="checkbx" name="chk1" id="chk1" value="09:00-11:00">

more info here:

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/checkbox

Then, you need to retrieve it:

var checkedValue = document.querySelector('.checkbx:checked').value;
var firebaseRef = firebase.database().ref('facility');
firebaseRef.push().set({
 timesession: checkedValue 
  });

more info here:

https://firebase.google.com/docs/database/web/read-and-write

Upvotes: 1

Related Questions