Show sourcecode
The following files exists in this folder. Click to view.
webbsrvprg/exercises/inlamningar/
formular.php
197 lines UTF-8 Windows (CRLF)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Formulär</title>
<style>
.correct { background-color: #c8e6c9; padding:4px; }
.wrong { background-color: #ffcdd2; padding:4px; }
.option { padding:4px; }
</style>
</head>
<body>
<?php
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
?>
<h1>Formulär</h1>
<?php
mb_language("uni");
mb_internal_encoding("UTF-8");
$logged_in = false;
$password = "Password123";
$entered_password = "";
$name = "";
$curr_question = 1;
$answers_csv = ""; // kommaseparerade val (index per fråga)
if (isset($_POST["curr_question"])) {
$curr_question = intval($_POST["curr_question"]);
}
if (isset($_POST["answers"])) {
$answers_csv = trim($_POST["answers"]);
}
if (isset($_POST["password"]) && !empty($_POST["password"])) {
if ($_POST["password"] === $password) {
$logged_in = true;
$entered_password = $_POST["password"];
} else {
echo "<p>Fel lösenord!</p>";
}
} elseif (isset($_POST["password"]) && empty($_POST["password"])) {
echo "<p>Vänligen ange ett lösenord.</p>";
}
// Frågor: text, alternativ, korrekt index (0-baserat)
$questions = [
[
'q' => 'Vad är 2+2?',
'options' => ['0','2','4','22'],
'correct' => 2
],
[
'q' => 'Var ligger VGY?',
'options' => ['Gullmars','Varmdo','Lulea','Manen'],
'correct' => 0
],
[
'q' => 'Vem är Tobias?',
'options' => ['Reinkarnation av Olof Palme','Rektor','Webblarare','Popstjarna'],
'correct' => 2
],
[
'q' => 'Vem grundade IKEA?',
'options' => ['Nicklas','Ingvar Kamprad','Folke Lundin','Rektor Kajsa'],
'correct' => 1
]
];
$total_questions = count($questions);
// Hantera inskickat svar (när en fråga besvaras)
if ($logged_in) {
if (!empty($_POST["name"])) {
$name = $_POST["name"];
} elseif (isset($_POST["name"])) {
$name = $_POST["name"]; // även om tomt (sällsynt)
}
// Om ett svar för aktuell fråga skickats med
if (isset($_POST["answer"]) && $_POST["answer"] !== "") {
// append chosen index till answers_csv
$chosen = intval($_POST["answer"]);
if ($answers_csv === "") {
$answers_csv = $chosen;
} else {
$answers_csv .= "," . $chosen;
}
$curr_question++; // gå vidare till nästa fråga (oavsett rätt/fel)
}
echo "<p>Du är inloggad!</p>";
if ($name === "") {
// Fråga efter namn först
echo "<form method='post' action=''>";
echo "<label for='name'>Ange ditt namn: </label>";
echo "<input type='text' id='name' name='name' required><br><br>";
echo "<input type='submit' value='Skicka'><br><br>";
echo "<input type='hidden' name='password' value='".htmlspecialchars($entered_password, ENT_QUOTES)."'>";
echo "</form>";
} else {
echo "<p>Hej " . htmlspecialchars($name, ENT_QUOTES) . "!</p>";
// Om vi är klara med alla frågor -> visa resultat och skicka mail
if ($curr_question > $total_questions) {
// Räkna ut resultat
$answers_arr = array_filter(array_map('intval', explode(',', $answers_csv)), function($v){ return $v !== null; });
$score = 0;
$result_lines = [];
for ($i = 0; $i < $total_questions; $i++) {
$correct_index = $questions[$i]['correct'];
$chosen_index = isset($answers_arr[$i]) ? $answers_arr[$i] : null;
$is_correct = ($chosen_index !== null && $chosen_index === $correct_index);
if ($is_correct) $score++;
$result_lines[] = [
'question' => $questions[$i]['q'],
'options' => $questions[$i]['options'],
'correct' => $correct_index,
'chosen' => $chosen_index
];
}
// Visa resultatöversikt
echo "<h2>Tack för dina svar!</h2>";
echo "<p>Du fick $score av $total_questions rätt.</p>";
// Visa varje fråga med markerade alternativ
foreach ($result_lines as $idx => $rl) {
echo "<div style='margin-bottom:12px;'>";
echo "<h3>Fråga " . ($idx+1) . ": " . htmlspecialchars($rl['question'], ENT_QUOTES) . "</h3>";
foreach ($rl['options'] as $opt_idx => $opt_text) {
$clas = 'option';
if ($opt_idx === $rl['correct']) $clas = 'correct';
if ($rl['chosen'] !== null && $opt_idx === $rl['chosen'] && $opt_idx !== $rl['correct']) $clas = 'wrong';
echo "<div class='". $clas ."'>" . htmlspecialchars($opt_text, ENT_QUOTES) . "</div>";
}
echo "</div>";
}
// Skicka mail med resultat (ändra mottagaradress efter behov)
$result_email = 'lovhol23@varmdogymnasium.se'; // <-- byt till riktig adress på servern
$subject = "Quizresultat: " . $name;
$body = "Namn: " . $name . "\n";
$body .= "Resultat: $score / $total_questions\n\n";
foreach ($result_lines as $i => $rl) {
$body .= "Fraga " . ($i+1) . ": " . $rl['question'] . "\n";
foreach ($rl['options'] as $o_idx => $o_text) {
$mark = ($o_idx === $rl['correct']) ? "(RATT)" : "";
$chosenMark = ($rl['chosen'] !== null && $o_idx === $rl['chosen'] && $o_idx !== $rl['correct']) ? "(DITT VAL - FEL)" : "";
$body .= "- " . $o_text . " $mark $chosenMark\n";
}
$body .= "\n";
}
// mail kan misslyckas på vissa testmiljöer; vi försöker skicka men visar inget fel till användaren
if (mb_send_mail('lovhol23@varmdogymnasium.se', $subject, $body)) {
echo "Meddelandet skickades!";
} else {
echo "Misslyckades att skicka meddelandet.";
}
// Möjlighet att starta om
echo "<form method='get' action=''>";
echo "<input type='submit' value='Gör om quizet'>";
echo "</form>";
} else {
// Visa aktuell fråga
$qindex = $curr_question - 1;
$question = $questions[$qindex];
echo "<h2>" . htmlspecialchars($question['q'], ENT_QUOTES) . "</h2>";
echo "<form method='post' action=''>";
echo "<label for='answer'>Välj svar: </label>";
echo "<select name='answer' id='answer'>";
foreach ($question['options'] as $opt_idx => $opt_text) {
echo "<option value='$opt_idx'>" . htmlspecialchars($opt_text, ENT_QUOTES) . "</option>";
}
echo "</select>";
echo "<input type='submit' value='Skicka'><br><br>";
// Skicka med sparade värden
echo "<input type='hidden' name='password' value='".htmlspecialchars($entered_password, ENT_QUOTES)."'>";
echo "<input type='hidden' name='name' value='".htmlspecialchars($name, ENT_QUOTES)."'>";
echo "<input type='hidden' name='curr_question' value='".htmlspecialchars($curr_question, ENT_QUOTES)."'>";
echo "<input type='hidden' name='answers' value='".htmlspecialchars($answers_csv, ENT_QUOTES)."'>";
echo "</form>";
}
}
} else {
// Inloggningsformulär
echo "<form method='post' action=''>";
echo "<label for='password'>Lösenord: </label>";
echo "<input type='password' id='password' name='password' required>";
echo "<input type='submit' value='Logga in'>";
echo "</form>";
}
?>
</body>
</html>