Show sourcecode
The following files exists in this folder. Click to view.
webbserverprogrammering/exercises/form_uppgift/
form_uppgift2.php
form_uppgift3.php
form_uppgift5.php
form_uppgift6.php
hash.php
form_uppgift3.php
225 lines UTF-8 Windows (CRLF)
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?php
// show all error reporting
error_reporting(-1); // Report all type of errors
ini_set('display_errors', 1); // Display all errors
ini_set('output_buffering', 0); // Do not buffer outputs, write directly
session_start();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Uppgift 3</title>
<style>
.flex {
display: flex;
align-items: center;
}
</style>
</head>
<body>
<?php
if (!isset($_SESSION["curr_q"])) {
$_SESSION["curr_q"] = 0;
}
$curr_q = $_SESSION["curr_q"];
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (!empty($_POST['q' . $_SESSION["curr_q"]])) {
$_SESSION["curr_q"]++;
} else if ($curr_q === 4) {
$_SESSION['curr_q']++;
}
$curr_q = $_SESSION["curr_q"];
if ($curr_q === 5) {
$_SESSION["curr_q"] = 0;
}
if ($curr_q > 3) {
?>
Ditt namn är <?php echo $_POST["q0"]; ?>.
<?php
if (isset($_POST["q0"]) && isset($_POST["q1"]) && isset($_POST["q2"]) && isset($_POST["q3"])) {
$correct_answers = 0;
if ($_POST["q1"] == '517') {
$correct_answers++;
}
if ($_POST["q2"] == 'Ja') {
$correct_answers++;
}
if ($_POST["q3"] == 'Kanske') {
$correct_answers++;
}
?>
Du fick <?php echo $correct_answers; ?> rätt.
<?php
}
}
}
$curr_q = $_SESSION["curr_q"];
?>
<form action="" method="post">
<input type="hidden" value="<?php echo $_POST["curr_q"] ?? 0; ?>" name="curr_q">
<?php
if ($curr_q === 0) {
?>
<label for="name">Namn:</label>
<input type="text" id="q0" name="q0" required>
<?php
} else {
?>
<input type="hidden" name="q0" id="q0" value="<?php echo $_POST["q0"] ?? ""; ?>">
<?php
}
if ($curr_q > 0 && $curr_q < 4) {
?>
<fieldset>
<legend>Frågor</legend>
<?php
}
?>
<?php
if ($curr_q === 1) {
?>
<div class="flex">
<label for="q1">Hur mycket väger Helge? (<span id="a1"></span> kg)</label>
<input type="range" name="q1" id="q1" min="100" max="999" required>
</div>
<?php
} else {
?>
<input type="hidden" value="<?php echo $_POST["q1"] ?? 100; ?>" name="q1" id="q1">
<?php
}
if ($curr_q === 2) {
?>
<div>
Är Helge en älg?
<div>
<input type="radio" name="q2" id="q2:1" value="Ja" required>
<label for="q2:1">Ja</label>
<input type="radio" name="q2" id="q2:2" value="Nej">
<label for="q2:2">Nej</label>
<input type="radio" name="q2" id="q2:3" value="Kanske">
<label for="q2:3">Kanske</label>
<input type="radio" name="q2" id="q2:4" value="Jag vet inte">
<label for="q2:4">Jag vet inte</label>
</div>
</div>
<?php
} else {
?>
<input type="hidden" name="q2" id="q2" value="<?php echo $_POST["q2"] ?? ""; ?>">
<?php
}
if ($curr_q === 3) {
?>
<div>
Är Per en helg?
<div>
<input type="radio" name="q3" id="q3:1" value="Ja" required>
<label for="q3:1">Ja</label>
<input type="radio" name="q3" id="q3:2" value="Nej">
<label for="q3:2">Nej</label>
<input type="radio" name="q3" id="q3:3" value="Kanske">
<label for="q3:3">Kanske</label>
<input type="radio" name="q3" id="q3:4" value="Jag vet inte">
<label for="q3:4">Jag vet inte</label>
</div>
</div>
<?php
} else {
?>
<input type="hidden" name="q3" id="q3" value="<?php echo $_POST["q3"] ?? ""; ?>">
<?php
}
if ($curr_q > 0 && $curr_q < 4) {
?>
</fieldset>
<?php
}
?>
<?php
if ($curr_q < 3) {
?>
<button type="submit">Nästa fråga</button>
<?php
} else if ($curr_q === 4) {
?>
<button type="submit">Nollställ</button>
<?php
} else {
?>
<button type="submit">Skicka svar</button>
<?php
}
?>
</form>
<script>
const q1 = document.getElementById('q1');
const a1 = document.getElementById('a1');
q1.addEventListener('input', () => {
updateAnswerDisplay();
});
updateAnswerDisplay()
function updateAnswerDisplay() {
a1.innerText = q1.value;
}
</script>
</body>
</html>