-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrushscript.js
83 lines (64 loc) · 2.29 KB
/
crushscript.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const firebaseConfig = {
apiKey: "AIzaSyARP2p48tmeZQWiPJv_Ho5ldOJVfC2BKAA",
authDomain: "hinder-7102d.firebaseapp.com",
databaseURL: "https://hinder-7102d-default-rtdb.asia-southeast1.firebasedatabase.app",
projectId: "hinder-7102d",
storageBucket: "hinder-7102d.appspot.com",
messagingSenderId: "219761938962",
appId: "1:219761938962:web:a331f962d7d5359bddfecb",
measurementId: "G-KNR89KP1RJ"
};
firebase.initializeApp(firebaseConfig);
const db = firebase.database();
const el = document.getElementById("crushForm");
if (el) {
el.addEventListener('submit', submitCrushForm);
}
function submitCrushForm(e) {
e.preventDefault();
var crushName = document.getElementById("crushInput").value;
saveCrushForm(crushName);
alert('Saved');
document.getElementById('crushForm').reset();
}
var selectElement = document.getElementById("suggestionsDropdown");
var crushersFormDB = db.ref("crushForm");
function saveCrushForm(crushName) {
var crushId = generateUniqueCrushId();
var crushRef = ref(crushersFormDB, crushId);
set(crushRef, {
crushName: crushName
}, (error) => {
if (error) {
console.error("Error saving crush data:", error);
} else {
console.log("Crush data saved successfully.");
}
});
}
function populateDropdown() {
var dbRef = firebase.database().ref("contactForm");
// Use ref() to get a database reference
dbRef.get().then((snapshot) => {
if (snapshot.exists()) {
const data = snapshot.val();
Object.entries(data).forEach(([enrollmentNumber, childSnapshot]) => {
var fullName = childSnapshot.fullName;
console.log("fullname is", fullName);
var optionElement = document.createElement('option');
optionElement.value = enrollmentNumber;
optionElement.textContent = fullName;
selectElement.appendChild(optionElement);
});
}
}).catch((error) => {
console.error("Error getting data:", error);
alert("Error getting data:", error.message);
});
}
window.addEventListener('load', () => {
populateDropdown();
});
function generateUniqueCrushId() {
return new Date().getTime().toString();
}