Show sourcecode
The following files exists in this folder. Click to view.
webbsrvprg/exercises/inlamning_1/
uppgifter1-3.php
68 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Frågor</title>
</head>
<body>
<?php
$q1 = [
"question" => "Vad heter Harry?",
"alts" => ["Harry", "Något annat", "Göran", "Hemligt"],
"correct" => 0,
];
$q2 = [
"question" => "1 + 2?",
"alts" => ["1", "2", "3", "4"],
"correct" => 2,
];
$q3 = [
"question" => "Korvstroganof?",
"alts" => ["Kanske det", "Absolut inte", "Kanske inte det", "Absolut"],
"correct" => 3,
];
$q4 = [
"question" => "Vilke är den tionde bokstaven i alfabetet?",
"alts" => ["H", "I", "J", "Ö"],
"correct" => 2,
];
$questions = [$q1, $q2, $q3, $q4];
$currentQuestion = 0;
$prev_answers = "";
if (isset($_GET["current"])) {
$currentQuestion = intval($_GET["current"]);
$newAns = htmlspecialchars(str_replace(' ', '_', $_GET["ans"])); // Byter ut alla mellanslag mot _ så att man kan använda mellanslag för att skilja på olika svar
$prev_answers = $_GET["prevAnswers"] . " " . $newAns; // Lägger på alla tidigare insamlade svar med den nya
}
echo('<form style="margin: 100px 35vw;" method="get">');
if ($currentQuestion == 0){
echo('<input type="text" name="ans" id="namn" placeholder="Harry" required><label for="namn">Skriv ditt namn</label><br><br>');
} else if ($currentQuestion <= count($questions)){
// Lägger ut den frågan som det är dags för
$qDict = $questions[$currentQuestion - 1];
echo('<fieldset><legend>' . $qDict["question"] . '</legend>');
for ($i = 0; $i < count($qDict["alts"]); $i += 1){
$alt = $qDict["alts"][$i];
echo('<input type="radio" name="ans" value="' . $i . '" id="q' . $currentQuestion . '_' . $i . '" required><label for="q' . $currentQuestion . '_' . $i . '">' . $alt . '</label><br><br>');
}
echo('</fieldset>');
}
// Lägger till gömda inputfälten
echo('<input type="hidden" name="prevAnswers" value ="' . $prev_answers . '">');
$currentQuestion += 1;
echo('<input type="hidden" name="current" value ="' . $currentQuestion . '">');
// Lägger ut submiten
if ($currentQuestion > count($questions)){
echo('<button type="submit">Skicka in</button>');
} else{
echo('<button type="submit">Nästa</button>');
}
?>
</form>
</body>
</html>