-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeedback.php
184 lines (159 loc) · 6.7 KB
/
feedback.php
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
// Start or resume the session
session_start();
if (!isset($_SESSION['csrf_token'])) {
// Generate and set the session token if not already set
$csrf_token = bin2hex(random_bytes(32));
$_SESSION['csrf_token'] = $csrf_token;
}
?>
<!DOCTYPE html>
<html lang="fr">
<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>Temoignages</title>
<link rel="stylesheet" href="./assets/style/style.css">
</head>
<body>
<section id="notifications"></section>
<?php include_once "./components/header.php" ?>
<div class="feedback-page">
<div class="feedback-img"><img src="./assets/src/feedback-img-1.png"></div>
<form id="feedbackForm" class="form-new container feedback-form">
<h2 class="span-12 primary uppercase">Ajouter un témoignage</h2>
<div class="span-6"><label>Nom</label>
<input id="client_nameInput" type="text" value="" required class="input-front" maxlength="150">
</div>
<div class="span-6"><label>Note</label>
<div class="rating">
<div class="star">☆</div>
<div class="star">☆</div>
<div class="star">☆</div>
<div class="star">☆</div>
<div class="star">☆</div>
</div>
<input id="ratingInput" type="number" value="" hidden>
</div>
<div class="span-6"><label>Commentaire</label>
<textarea id="commentInput" rows="5" cols="50" required class="input-front"></textarea>
</div>
<select id="statusInput" hidden>
<option value="2">New</option>
</select>
<input type="hidden" name="csrf_token" id="csrf_token" value="<?php echo $_SESSION['csrf_token']; ?>">
<div class="button-container span-2-end">
<button class="btn" type="reset" id="reset-btn">Annuler</button>
<button class="btn" type="submit">Envoyer</button>
</div>
</form>
<div class="feedback-img"><img src="./assets/src/feedback-img-2.png"></div>
</div>
<?php include_once "./components/footer.php" ?>
<script src="./components/script-menu.js"></script>
<script src="./assets/js/notifications.js"></script>
<script>
//convert number into stars
const stars = document.querySelectorAll('.star');
const ratingInput = document.getElementById('ratingInput')
stars.forEach((star, index) => {
star.addEventListener('click', () => {
fillStars(stars, index);
ratingInput.value = index + 1
});
});
function fillStars(stars, selectedIndex) {
for (let i = 0; i <= selectedIndex; i++) {
stars[i].innerText = '★';
}
for (let i = selectedIndex + 1; i < stars.length; i++) {
stars[i].innerText = '☆';
}
}
function pushNewFeedback(formData, feedbackForm) {
const phpScriptURL = './admin/functions/db_query.php?action=newFeedback';
formData.action = 'newFeedback'
// Send an AJAX request to update the database
fetch(phpScriptURL, {
method: 'POST',
body: JSON.stringify(formData), // Send data as JSON
headers: {
'Content-Type': 'application/json',
},
})
.then((response) => response.json()) // Assuming the server returns JSON
.then((data) => {
notificationsServeur(data)
feedbackForm.reset()
stars.forEach(star => {
star.innerText = `☆`
})
})
.catch((error) => {
console.error("Error:", error);
});
}
//the stars in the feedback form
function isRatingSelected(stars) {
// Check if at least one star is selected
for (const star of stars) {
if (star.innerText === '★') {
return true; // At least one star is selected
}
}
return false; // No star is selected
}
function formvalidation() {
let valid = true
const clientNameInput = document.getElementById('client_nameInput');
const ratingInput = document.getElementById('ratingInput');
const commentInput = document.getElementById('commentInput');
// Validate client name (required, max length)
if (clientNameInput.value.trim() === '' || clientNameInput.value.length > 150) {
alert('Veuillez entrer votre nom.');
valid = false
}
// Validate rating (at least one star required)
if (ratingInput.value < 1) {
alert('Veuillez sélectionner une évaluation d\'au moins 1 étoile.');
valid = false
}
// Validate comment (required)
if (commentInput.value.trim() === '') {
alert('Veuillez entrer un commentaire.');
valid = false
}
return valid
}
const feedbackForm = document.getElementById('feedbackForm')
// Add a submit event listener to the form
feedbackForm.addEventListener('submit', (e) => {
e.preventDefault();
if (!isRatingSelected(stars)) {
alert('Please select a star rating before submitting.');
} else {
if (formvalidation() == true) {
let formData = {}
formData.userId = ""
// Iterate through all input and select elements
feedbackForm.querySelectorAll('input, select, textarea').forEach(input => {
// Handle other input and select elements
formData[input.id.replace("Input", "")] = input.value;
});
pushNewFeedback(formData, feedbackForm)
};
}
});
const resetButton = document.getElementById('reset-btn');
resetButton.addEventListener('click', function(e) {
e.preventDefault(); // Prevent the form from submitting
// Reset the form
feedbackForm.reset();
stars.forEach(star => {
star.innerText = `☆`
})
});
</script>
</body>
</html>