-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloudCRUD.html
149 lines (142 loc) · 5.17 KB
/
cloudCRUD.html
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
label{
display: block;
width: 80px;
}
</style>
</head>
<body>
<label>Name</label><input id="Namebox" type="text">
<label>RollNo</label><input id="Rollbox" type="text">
<label>Section</label><input id="Secbox" type="text">
<label>Name</label>
<select id="Genbox">
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
<hr>
<button id="Insbtn">INSERT</button>
<button id="Selbtn">SELECT</button>
<button id="Updbtn">UPDATE</button>
<button id="Delbtn">DELETE</button>
<script type="module">
// Import the functions you need from the SDKs you need
import { initializeApp } from "https://www.gstatic.com/firebasejs/9.6.7/firebase-app.js";
// TODO: Add SDKs for Firebase products that you want to use
// https://firebase.google.com/docs/web/setup#available-libraries
// Your web app's Firebase configuration
const firebaseConfig = {
apiKey: "AIzaSyDdv5VkG1Go61pCGTAWiMvVUIwKk5ZkM40",
authDomain: "fire9db-7dc00.firebaseapp.com",
projectId: "fire9db-7dc00",
storageBucket: "fire9db-7dc00.appspot.com",
messagingSenderId: "1085566079164",
appId: "1:1085566079164:web:d7f4cfca7f21b991f100b9"
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
import{
getFirestore,doc,setDoc,getDoc,collection,addDoc,updateDoc,deleteDoc,deleteField
}
from "https://www.gstatic.com/firebasejs/9.6.7/firebase-firestore.js";
const db=getFirestore();
let Namebox=document.getElementById("Namebox");
let Rollbox=document.getElementById("Rollbox");
let Secbox=document.getElementById("Secbox");
let Genbox=document.getElementById("Genbox");
let insBtn=document.getElementById("Insbtn");
let selBtn=document.getElementById("Selbtn");
let updBtn=document.getElementById("Updbtn");
let delBtn=document.getElementById("Delbtn");
async function AddDocument_AutoID(){
var ref=collection(db,"TheStudentList");
const docRef=await addDoc(
ref,{
NameOfStd:Namebox.value,
RollNo:Rollbox.value,
Section:Secbox.value,
Gender:Genbox.value
}
)
.then(()=>{
alert("Data added successfully");
})
.catch((error)=>{
alert("Unsuccessful operation,error:"+error);
});
}
async function AddDocument_CustomID(){
var ref=doc(db,"TheStudentList",Rollbox.value);
await setDoc(
ref,{
NameOfStd:Namebox.value,
RollNo:Rollbox.value,
Section:Secbox.value,
Gender:Genbox.value
}
)
.then(()=>{
alert("Data added successfully");
})
.catch((error)=>{
alert("Unsuccessful operation,error:"+error);
});
}
// insBtn.addEventListener("click",AddDocument_AutoID);
async function GetADocument(){
var ref=doc(db,"TheStudentList",Rollbox.value);
const docSnap=await getDoc(ref);
if(docSnap.exists()){
Namebox.value=docSnap.data().NameOfStd;
Secbox.value=docSnap.data().Section;
Genbox.value=docSnap.data().Gender;
}
else{
alert("No such Document")
}
}
async function UpdateFieldsInADocument(){
var ref=doc(db,"TheStudentList",Rollbox.value);
await updateDoc(
ref,{
NameOfStd:Namebox.value,
Section:Secbox.value,
Gender:Genbox.value
}
)
.then(()=>{
alert("Data updated successfully");
})
.catch((error)=>{
alert("Unsuccessful operation,error:"+error);
});
}
async function DeleteDocument(){
var ref=doc(db,"TheStudentList",Rollbox.value);
const docSnap=await getDoc(ref);
if(!docSnap.exists()){
alert("document does not exist");
return;
}
await deleteDoc(ref)
.then(()=>{
alert("data deleted successfully");
})
.catch((error)=>{
alert("unsuccessful operation,error:"+error);
})
}
insBtn.addEventListener("click",AddDocument_CustomID);
selBtn.addEventListener("click",GetADocument);
updBtn.addEventListener("click",UpdateFieldsInADocument);
delBtn.addEventListener("click",DeleteDocument);
</script>
</body>
</html>