-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript3.js
More file actions
79 lines (67 loc) · 2.66 KB
/
script3.js
File metadata and controls
79 lines (67 loc) · 2.66 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
import { initializeApp } from "https://www.gstatic.com/firebasejs/11.4.0/firebase-app.js";
import { getDatabase, ref, push } from "https://www.gstatic.com/firebasejs/11.4.0/firebase-database.js";
import { getAnalytics } from "https://www.gstatic.com/firebasejs/11.4.0/firebase-analytics.js";
// Firebase configuration
const firebaseConfig = {
apiKey: "AIzaSyCLYUrTFYHdHHvgl6A7YHUYgd_KeiMtXlQ",
authDomain: "udaya-ff6f8.firebaseapp.com",
databaseURL: "https://udaya-ff6f8-default-rtdb.firebaseio.com",
projectId: "udaya-ff6f8",
storageBucket: "udaya-ff6f8.appspot.com",
messagingSenderId: "626537740000",
appId: "1:626537740000:web:872021541143a61f8a4285",
measurementId: "G-7C3GFE8H8S"
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
const database = getDatabase(app);
const analytics = getAnalytics(app);
// Wait for the DOM to load before attaching event listener
document.addEventListener("DOMContentLoaded", function () {
const form = document.getElementById("details-form");
if (form) {
form.addEventListener("submit", submitForm);
} else {
console.error("Form not found!");
}
});
// Submit Form Function
async function submitForm(e) {
e.preventDefault();
// Get values
var name = getElementVal("name");
var emailid = getElementVal("email");
var mobile = getElementVal("mobile");
var price = document.getElementById("totalPrice").innerText;
var address = getElementVal("address");
var msgContent = "Order placed successfully!";
console.log("Submitting: ", { name, emailid, mobile, price, address, msgContent });
try {
// Save data to Firebase
await saveMessages(name, emailid, mobile, price, address, msgContent);
// Show success alert
alert("Your order is successful! 🎉");
// Reset form & show animation
document.getElementById("details-form").reset();
showCelebration();
} catch (error) {
console.error("Error saving data:", error);
alert("Failed to place order. Please try again.");
}
}
// Save Messages to Firebase
function saveMessages(name, emailid, mobile, price, address, msgContent) {
return push(ref(database, "details"), {
name: name,
emailid: emailid,
mobile: mobile,
price: price,
address: address,
msgContent: msgContent,
});
}
// Helper Function to Get Input Value
const getElementVal = (id) => document.getElementById(id)?.value || "";
function showCelebration() {
document.getElementById("celebration").style.display = "block";
}