Källkod
Välj en PHP-fil för att visa dess källkod.
- exercises.php
- exercises/extra_ovning.php
- exercises/hej_svejs.php
- exercises/index.php
- exercises/inkludera_1.php
- exercises/inkludera_2_sida1.php
- exercises/inkludera_2_sida2.php
- exercises/inkludera_2_sida3.php
- exercises/ovn_funk1.php
- exercises/ovn_funk2.php
- exercises/ovn_funk3.php
- exercises/ovn_funk4.php
- exercises/ovn_funk5.php
- exercises/ovn_funk6.php
- exercises/ovn_funk7.php
- exercises/ovn_funk8.php
- exercises/ovn_funk9.php
- exercises/ovn_gr1.php
- exercises/ovn_gr2.php
- exercises/ovn_gr3.php
- exercises/ovn_gr4.php
- exercises/ovn_gr5.php
- exercises/ovn_gr6.php
- exercises/ovn_gr7.php
- exercises/uppg3.php
- exercises/uppg5.php
- incl/config.php
- incl/footer.php
- incl/header.php
- index.php
- source.php
- viewsource.php
exercises/uppg3.php
<?php
require __DIR__ . '/../incl/config.php';
error_reporting(-1);
ini_set('display_errors', '1');
function h($value)
{
return htmlspecialchars((string)$value, ENT_QUOTES, 'UTF-8');
}
$questions = [
[
'question' => 'Vilket språk körs på webbservern i den här kursen?',
'answers' => ['a' => 'PHP', 'b' => 'CSS', 'c' => 'HTML', 'd' => 'PNG'],
'correct' => 'a',
],
[
'question' => 'Vilken superglobal används för formulär som skickas med POST?',
'answers' => ['a' => '$_GET', 'b' => '$_POST', 'c' => '$_FILES', 'd' => '$_COOKIE'],
'correct' => 'b',
],
[
'question' => 'Vilken funktion hjälper till att visa användartext säkert i HTML?',
'answers' => ['a' => 'array_sum()', 'b' => 'mail()', 'c' => 'htmlspecialchars()', 'd' => 'sort()'],
'correct' => 'c',
],
];
$step = 1;
$name = '';
$email = '';
$emailOptIn = false;
$answers = [1 => '', 2 => '', 3 => ''];
$errors = [];
$finished = false;
$score = 0;
$emailStatus = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$postedStep = filter_var($_POST['step'] ?? 1, FILTER_VALIDATE_INT);
if ($postedStep !== false && $postedStep >= 1 && $postedStep <= 3) {
$step = $postedStep;
}
$name = trim($_POST['name'] ?? '');
$email = trim($_POST['email'] ?? '');
$emailOptIn = isset($_POST['email_opt_in']);
for ($i = 1; $i <= 3; $i++) {
$answers[$i] = trim($_POST['a' . $i] ?? '');
}
$choice = trim($_POST['answer'] ?? '');
if ($name === '') {
$errors[] = 'Skriv ditt namn innan du fortsätter.';
}
if ($emailOptIn && !filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors[] = 'Skriv en giltig e-postadress eller avmarkera mejlvalet.';
}
if (!array_key_exists($choice, $questions[$step - 1]['answers'])) {
$errors[] = 'Välj ett av svarsalternativen.';
}
if (!$errors) {
$answers[$step] = $choice;
if ($step < count($questions)) {
$step++;
} else {
$finished = true;
foreach ($questions as $index => $question) {
if ($answers[$index + 1] === $question['correct']) {
$score++;
}
}
if ($emailOptIn) {
$message = "Hej $name!\n\nDu fick $score av " . count($questions) . " rätt i frågetävlingen.";
if (mail($email, 'Ditt resultat i frågetävlingen', $message)) {
$emailStatus = 'Resultatet har skickats till din e-postadress.';
} else {
$emailStatus = 'Resultatet kunde inte mejlas från den här servern.';
}
}
}
}
}
$baseUrl = '../';
$title = 'Frågetävling – uppgift 3';
require __DIR__ . '/../incl/header.php';
?>
<section class="quiz-card">
<h1>Frågetävling</h1>
<p>Svara på tre frågor. Bara en fråga visas åt gången.</p>
<?php if ($errors): ?>
<p class="error"><?= h(implode(' ', $errors)) ?></p>
<?php endif; ?>
<?php if ($finished): ?>
<h2>Resultat</h2>
<p><?= h($name) ?> fick <strong><?= $score ?> av <?= count($questions) ?></strong> rätt.</p>
<?php if ($emailStatus !== ''): ?><p><?= h($emailStatus) ?></p><?php endif; ?>
<a class="button" href="uppg3.php">Spela igen</a>
<?php else: ?>
<h2>Fråga <?= $step ?> av <?= count($questions) ?></h2>
<form method="post">
<input type="hidden" name="step" value="<?= $step ?>">
<?php for ($i = 1; $i <= 3; $i++): ?>
<?php if ($answers[$i] !== ''): ?>
<input type="hidden" name="a<?= $i ?>" value="<?= h($answers[$i]) ?>">
<?php endif; ?>
<?php endfor; ?>
<?php if ($step === 1): ?>
<label for="name">Ditt namn
<input id="name" type="text" name="name" value="<?= h($name) ?>" required>
</label>
<label>
<input type="checkbox" name="email_opt_in" value="yes" <?= $emailOptIn ? 'checked' : '' ?>>
Mejla gärna mitt resultat
</label>
<label for="email">E-postadress (valfri)
<input id="email" type="email" name="email" value="<?= h($email) ?>">
</label>
<?php else: ?>
<input type="hidden" name="name" value="<?= h($name) ?>">
<input type="hidden" name="email" value="<?= h($email) ?>">
<?php if ($emailOptIn): ?><input type="hidden" name="email_opt_in" value="yes"><?php endif; ?>
<?php endif; ?>
<p><?= h($questions[$step - 1]['question']) ?></p>
<?php foreach ($questions[$step - 1]['answers'] as $key => $label): ?>
<label>
<input type="radio" name="answer" value="<?= h($key) ?>" required>
<?= h($label) ?>
</label>
<?php endforeach; ?>
<button type="submit">Nästa</button>
</form>
<?php endif; ?>
</section>
<?php require __DIR__ . '/../incl/footer.php'; ?>