-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessages.js
104 lines (87 loc) · 3.18 KB
/
messages.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
//-----Messages--------
document.addEventListener("DOMContentLoaded", function () {
// Retrieve form data from local storage
const formSubmissionsString = localStorage.getItem("formSubmissions");
const submissionTable = document.getElementById("submissionTable");
const messageCount = document.getElementById("messageCount");
if (formSubmissionsString) {
// Parse form submissions from JSON string
const formSubmissions = JSON.parse(formSubmissionsString);
// Populate table with form submissions
/*var number_messages = formSubmissions.length;
messageCount.textContent = number_messages;*/
formSubmissions.forEach((formData, index) => {
const row = document.createElement("tr");
row.innerHTML = `
<td>${index + 1}</td>
<td>${formData.name}</td>
<td>${formData.email}</td>
<td>${formData.message}</td>
<td><button><a href=mailto:${
formData.email
}>Reply</a></button> </td>
<td><button onclick="deleting(${index})">Delete</button></td>
`;
submissionTable.appendChild(row);
});
}
});
function submitForm() {
// Get form data
const name = document.getElementById("name").value.trim();
const email = document.getElementById("email").value.trim();
const message = document.getElementById("message").value.trim();
// Validate form data
if (name === "" || email === "" || message === "") {
document.getElementById("Error").innerHTML = "Please fill in all fields.";
return;
}
if (!isValidEmail(email)) {
document.getElementById("Error").innerHTML =
"Please enter a valid email address.";
return;
}
// Retrieve form submissions from local storage
const formSubmissionsString = localStorage.getItem("formSubmissions");
let formSubmissions = [];
if (formSubmissionsString) {
formSubmissions = JSON.parse(formSubmissionsString);
}
// Construct an object to hold form data
const formData = {
name: name,
email: email,
message: message,
};
// Add new form data to existing submissions
formSubmissions.push(formData);
// Save updated form submissions to local storage
localStorage.setItem("formSubmissions", JSON.stringify(formSubmissions));
// Redirect to the dashboard
window.location.href = "messages.html";
}
// Function to validate email format
function isValidEmail(email) {
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return emailRegex.test(email);
}
function deleting() {
var result = confirm("Do you want to delete this message?");
if (result) {
deleteEntry();
}
}
function deleteEntry(index) {
// Retrieve form submissions from local storage
const formSubmissionsString = localStorage.getItem("formSubmissions");
if (formSubmissionsString) {
// Parse form submissions from JSON string
let formSubmissions = JSON.parse(formSubmissionsString);
// Remove entry at the specified index
formSubmissions.splice(index, 1);
// Save updated form submissions to local storage
localStorage.setItem("formSubmissions", JSON.stringify(formSubmissions));
// Refresh the page to reflect changes
window.location.reload();
}
}