-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathindex.js
More file actions
88 lines (65 loc) · 2.13 KB
/
Copy pathindex.js
File metadata and controls
88 lines (65 loc) · 2.13 KB
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
84
85
86
87
88
// Your web app's Firebase configuration
const firebaseConfig = {
"%your-config%":"xxx"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
// Set database variable
var database = firebase.database()
function save() {
var location = document.getElementById('location').value
var crime = document.getElementById('crime').value
database.ref('crimes/' + location).set({
location: location,
crime: crime
})
alert('Saved')
resetForm();
}
function get() {
var users_ref = database.ref('crimes/');
var table = document.createElement('table');
var tbody = document.createElement('tbody');
users_ref.on('value', function(snapshot) {
snapshot.forEach(function(childSnapshot) {
var data = childSnapshot.val();
var location = childSnapshot.key;
var crime = data.crime;
var row = document.createElement('tr');
var locationCell = document.createElement('td');
var crimeCell = document.createElement('td');
locationCell.textContent = location;
crimeCell.textContent = crime;
row.appendChild(locationCell);
row.appendChild(crimeCell);
tbody.appendChild(row);
});
table.appendChild(tbody);
document.body.appendChild(table);
});
resetForm();
}
function update() {
var location = document.getElementById('location').value
var crime = document.getElementById('crime').value
var updates = {
location: location,
crime : crime
}
database.ref('crimes/' + location).update(updates)
alert('updated')
resetForm();
}
function remove() {
var location = document.getElementById('location').value
database.ref('crimes/' + location).remove()
alert('deleted')
resetForm();
}
function resetForm() {
document.getElementById('email').value = '';
document.getElementById('password').value = '';
document.getElementById('username').value = '';
document.getElementById('say_something').value = '';
document.getElementById('favourite_food').value = '';
}