vendredi 25 août 2017

how to get the value of options from select form and submit it to firebase

I would like to get the value of the selected option and insert in into my firebase databae.

var config = {
    apiKey: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    authDomain: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    databaseURL: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    projectId: "xxxxxxxxxxxxxxxxxxxx",
    storageBucket: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    messagingSenderId: "xxxxxxxxxxxxxxxxxxxx"
  };
  firebase.initializeApp(config);

var messagesRef = firebase.database().ref('choices');

document.getElementById('personForm').addEventListener('submit',submitForm);

function submitForm(e){
    e.preventDefault();  
    //get input values
    var name = getInputVal('name');
    
    var x  = document.getElementById('career');
    const career = x.options[x.selectedIndex].text;
   saveMessage(name,career);
    document.getElementById('select').reset(); 
}

 function getInputVal(id){
     return document.getElementById(id).value;
}

function saveMessage(name, career){
    var newMessageRef = messagesRef.push();
    newMessageRef.set({
      name: name,
      career:career
    });
  }
<form id="select">
  <div class="field">
    <label class="label">Name</label>
    <div class="control">
      <input class="input is-primary" type="text" placeholder="" id="name">
    </div>
  </div>
  <div class="field">
    <label class="label">Career</label>
    <div class="control">
      <div class="select is-primary">
        <select id="career">
          <option>Option 1</option>
          <option>Option 2</option>
          <option>Option 3</option>
        </select>
      </div>
    </div>
  </div>
  <button class="button is-primary is-outlined" type="submit">Ajouter</button>
</form>

when I remove the select field it works correctly but doesn't show save anything when submite with the select field.

Thanks in advance for your help




Aucun commentaire:

Enregistrer un commentaire