Show sourcecode
The following files exists in this folder. Click to view.
webbserverprogrammering/submissions/inlamning_formular/
del_1-3.php
del_4-5.php
style.css
del_4-5.php
180 lines UTF-8 Windows (CRLF)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/dark.css">
<title>Inlämning Formulär 1-3</title>
<style>
body{
width: 100%;
min-height: 90vh;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
</style>
</head>
<body>
<?php
//session to keep track of answers
session_start();
//password protection
$quizPassword = "Tobias!";
if (!isset($_SESSION['authenticated'])) {
if (isset($_POST['quiz_password'])) {
if ($_POST['quiz_password'] === $quizPassword) {
$_SESSION['authenticated'] = true;
} else {
echo '<form method="post"><label>Lösenord (Tobias!): <input type="password" name="quiz_password" required></label><button type="submit">Logga in</button></form>';
echo '<p style="color:red;">Fel lösenord!</p>';
exit;
}
} else {
echo '<form method="post"><label>Lösenord: <input type="password" name="quiz_password" required></label><button type="submit">Logga in</button></form>';
exit;
}
}
$step = isset($_POST['step']) ? intval($_POST['step']) : 0;
$name = isset($_POST['name']) ? htmlspecialchars($_POST['name']) : (isset($_SESSION['name']) ? $_SESSION['name'] : '');
$email = isset($_POST['email']) ? htmlspecialchars($_POST['email']) : (isset($_SESSION['email']) ? $_SESSION['email'] : '');
$_SESSION['name'] = $name;
$_SESSION['email'] = $email;
//correct answers and questions
$questions = [
[
'text' => 'Fråga 1: Kollar Tobias på anime?',
'options' => ['a' => 'Absolut inte', 'b' => 'Ja, varje dag', 'c' => 'Malmö', 'd' => 'Bara med Niclas'],
'correct' => ['b'],
'type' => 'radio'
],
[
'text' => 'Fråga 2: Gillar Tobias anime mer än VGY?',
'options' => ['a' => 'Ja', 'b' => 'Nej', 'c' => 'ばか'],
'correct' => ['a'],
'type' => 'radio'
],
[
'text' => 'Fråga 3: Hur många gånger har Tobias sagt åt Niclas att titta på anime?',
'options' => ['a' => 'Aldrig', 'b' => '3 gånger', 'c' => 'Niclas Rasmusson', 'd' => '5'],
'correct' => ['d'],
'type' => 'radio'
],
[
'text' => 'Fråga 4: Vilken är Tobias favoritanime?',
'options' => ['a' => 'VGY Harem', 'b' => 'I died and got, reincarnated as Niclas Rasmusson', 'c' => 'JavaScript dungeon adventures', 'd' => 'Attack on HTML'],
'correct' => ['a', 'c'],
'type' => 'checkbox'
]
];
//user answers
if ($step > 0 && (isset($_POST['answer']) || isset($_POST['answer_multi']))) {
if (!isset($_SESSION['answers'])) {
$_SESSION['answers'] = [];
}
if (isset($_POST['answer_multi'])) {
$_SESSION['answers'][] = $_POST['answer_multi'];
} else {
$_SESSION['answers'][] = $_POST['answer'];
}
}
if ($step == 0) {
//name/email form
?>
<form method="post">
<label>Namn: <input type="text" name="name" required></label><br>
<label>Epost: <input type="email" name="email" required></label><br>
<input type="hidden" name="step" value="1">
<button type="submit">Starta Quiz</button>
</form>
<?php
} else if ($step > 0 && $step <= count($questions)) {
$q = $questions[$step-1];
?>
<form method="post">
<p><?php echo $q['text']; ?></p>
<?php if ($q['type'] === 'radio'): ?>
<?php foreach ($q['options'] as $key => $val): ?>
<label><input type="radio" name="answer" value="<?php echo $key; ?>" required> <?php echo $val; ?></label><br>
<?php endforeach; ?>
<?php else: ?>
<?php foreach ($q['options'] as $key => $val): ?>
<label><input type="checkbox" name="answer_multi[]" value="<?php echo $key; ?>"> <?php echo $val; ?></label><br>
<?php endforeach; ?>
<?php endif; ?>
<input type="hidden" name="step" value="<?php echo $step+1; ?>">
<button type="submit"><?php echo $step == count($questions) ? 'Skicka in' : 'Nästa'; ?></button>
</form>
<?php
} else if ($step == count($questions)+1) {
//result
$answers = isset($_SESSION['answers']) ? $_SESSION['answers'] : [];
$score = 0;
for ($i = 0; $i < count($questions); $i++) {
$q = $questions[$i];
$userAns = isset($answers[$i]) ? $answers[$i] : [];
if ($q['type'] === 'radio') {
if (is_array($userAns)) $userAns = reset($userAns); // just in case
if (in_array($userAns, $q['correct'])) {
$score++;
}
} else if ($q['type'] === 'checkbox') {
if (!is_array($userAns)) $userAns = [$userAns];
sort($userAns);
$correctSorted = $q['correct'];
sort($correctSorted);
if ($userAns == $correctSorted) {
$score++;
}
}
}
echo "<h2>Resultat</h2>";
echo "Spelare: $name<br>";
echo "Antal korrekta svar: $score av ".count($questions)."<br>";
//questions and answers with color
echo '<div style="text-align:left;">';
for ($i = 0; $i < count($questions); $i++) {
$q = $questions[$i];
$userAns = isset($answers[$i]) ? $answers[$i] : [];
echo '<p><strong>'.$q['text'].'</strong></p>';
foreach ($q['options'] as $key => $val) {
$color = '';
if (in_array($key, $q['correct'])) {
$color = 'background-color: #90EE90; color: black;'; // green
}
if ($q['type'] === 'radio') {
if (is_array($userAns)) $userAns = reset($userAns);
if ($userAns == $key && !in_array($key, $q['correct'])) {
$color = 'background-color: #FF6347; color: black'; // red
}
} else if ($q['type'] === 'checkbox') {
if (is_array($userAns) && in_array($key, $userAns) && !in_array($key, $q['correct'])) {
$color = 'background-color: #FF6347; color: black'; // red
}
}
echo '<span style="display:inline-block; margin:2px; padding:2px 8px; border-radius:4px; '.$color.'">'.$val.'</span> ';
}
echo '<br>';
}
echo '</div>';
//email
$to = $email;
$subject = "Quizresultat";
$message = "Hej $name!\nDu fick $score av ".count($questions)." rätt på quizet.";
mail($to, $subject, $message);
//clear session
session_destroy();
}
?>
</body>
</html>