-
Notifications
You must be signed in to change notification settings - Fork 0
/
vote.php
120 lines (110 loc) · 2.83 KB
/
vote.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
<?php
session_start();
if (!isset($_SESSION['logged_in']) || !$_SESSION['logged_in']) {
header('Location: login.php');
exit;
}
$errors = [];
$poll_id = $_GET['poll_id'] ?? null;
$message = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$options = $_POST['options'] ?? [];
if (!$options) {
$errors[] = 'You must select at least one option.';
} else {
}
if (!$errors) {
$message = 'SUCCESS';
}
}
if(file_exists('polls.json')){
$polls = json_decode(file_get_contents('polls.json'), true);
$poll = null;
foreach ($polls as $p) {
if ($p['id'] == $poll_id) {
$poll = $p;
break;
}
}
if (!$poll) {
echo 'Poll not found.';
exit;
}
}else{
echo 'Polls not found.';
exit;
}
?>
<div>
<h2>Poll: <?= htmlspecialchars($poll['question']) ?></h2>
<p>expiry: <?= htmlspecialchars($poll['expiry']) ?></p>
<p>Time of creation: <?= htmlspecialchars($poll['startedAt']) ?></p>
<form method="post">
<ul>
<?php foreach ($poll['options'] as $opt): ?>
<li>
<input type="<?= $poll['multiple_choice'] ? 'checkbox' : 'radio'?>" id="<?= $opt['id'] ?>" name="options[]" value="<?= $opt['id'] ?>" >
<label for="<?= $opt['id'] ?>"><?= htmlspecialchars($opt['text']) ?></label>
</li>
<?php endforeach; ?>
</ul>
<?php if ($errors): ?>
<div class="error">
<ul>
<?php foreach ($errors as $error): ?>
<li><?= $error ?></li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
<?php if (!empty($message)): ?>
<div class="message"><?= $message ?></div>
<?php endif; ?>
<input type="submit" value="Vote">
</form>
<div>
<a href="index.php">Back to polls</a>
</div>
</div>
<script>
var multiple_choice = <?= $poll['multiple_choice'] ?>;
if(!multiple_choice){
var radios = document.getElementsByName('options[]');
for (var i = 0, length =radios.length; i < length; i++){
radios[i].onclick = function() {
for (var i = 0; i < radios.length; i++) {
radios[i].checked = false;
}
this.checked = true;
};
}
}
</script>
<?php
if(isset($_POST['options'])) {
if(empty($_POST['options'])) {
$errors[] = "Please select an option.";
} else {
}
if (!$errors) {
$message = 'SUCCESS';
}
}
if(file_exists('polls.json')){
$polls = json_decode(file_get_contents('polls.json'), true);
$poll = null;
foreach ($polls as $p) {
if ($p['id'] == $poll_id) {
$poll = $p;
break;
}
}
if (!$poll) {
echo 'Poll not found.';
exit;
}
}else{
echo 'Polls not found.';
exit;
}
?>