Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save spac3unit/3e59ee3a0dacac940dc43913d91ba19b to your computer and use it in GitHub Desktop.

Select an option

Save spac3unit/3e59ee3a0dacac940dc43913d91ba19b to your computer and use it in GitHub Desktop.
firebase-by-example: Realtime DB
// Credits to the awesome Brad Traversy, check out this video at: https://www.youtube.com/watch?v=PP4Tr0l08NE
var messagesRef = firebase.database().ref('messages');
document.getElementById('contactForm').addEventListener('submit', submitForm);
function submitForm(e) {
e.preventDefault(); //prevent reloading the html page
var name = getInputVal('name');
var email = getInputVal('email');
var company = getInputVal('company');
var phone = getInputVal('phone');
var message = getInputVal('message');
saveMessageToRealtimeDB(name, company, email, phone, message);
saveFormToFirestore(name, company, email, phone, message);
document.querySelector('.alert').style.display = 'block';
setTimeout(function () {
document.querySelector('.alert').style.display = 'none';
}, 3000);
document.getElementById('contactForm').reset();
}
function getInputVal(id) {
return document.getElementById(id).value;
}
function saveMessageToRealtimeDB(name, company, email, phone, message) {
var newMessageRef = messagesRef.push();
newMessageRef.set({
name: name,
company: company,
email: email,
phone: phone,
message: message
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment