-
Notifications
You must be signed in to change notification settings - Fork 1
/
form.js
42 lines (36 loc) · 1009 Bytes
/
form.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Reference messages collection
var messagesRef = firebase.database().ref('Messages');
// Listen for form submit
document.getElementById('reused_form').addEventListener('submit', submitForm);
// Submit form
function submitForm(e){
e.preventDefault();
// Get values
var message = getInputVal('message');
var name = getInputVal('name');
var email = getInputVal('email');
// Save message
saveMessage(message, name, email);
var user = firebase.auth().currentUser;
if (user) {
alert("Message has been recieved. We will get back to you soon. ");
window.location.replace("./index.html");
}
else {
alert("Login First");
window.location.replace("./index.html");
}
}
// Function to get get form values
function getInputVal(id){
return document.getElementById(id).value;
}
// Save message to firebase
function saveMessage(message, name, email){
var newMessageRef = messagesRef.push();
newMessageRef.set({
Message: message,
Name: name,
Email:email
});
}