<?php
// サーバー側で問題を管理
$questions = [
[
"question" => "世界で一番高い山は?",
"options" => ["富士山", "エベレスト", "キリマンジャロ"],
"answer" => "エベレスト"
],
[
"question" => "光の速さはおよそ秒速何km?",
"options" => ["30万km", "3万km", "300万km"],
"answer" => "30万km"
],
[
"question" => "1年は何日?(うるう年を除く)",
"options" => ["365日", "364日", "366日"],
"answer" => "365日"
],
[
"question" => "日本で一番大きい湖は?",
"options" => ["琵琶湖", "霞ヶ浦", "サロマ湖"],
"answer" => "琵琶湖"
],
[
"question" => "太陽系で一番大きい惑星は?",
"options" => ["木星", "土星", "地球"],
"answer" => "木星"
]
];
// ランダムに1問選択
$randomIndex = array_rand($questions);
$selectedQuestion = $questions[$randomIndex];
?>
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ランダムクイズ - 演習3</title>
<style>
body {
font-family: sans-serif;
max-width: 600px;
margin: 50px auto;
padding: 20px;
background-color: #f5f5f5;
}
.quiz-container {
background: white;
padding: 30px;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
h1 {
color: #333;
margin-bottom: 30px;
}
.question {
margin-bottom: 20px;
font-size: 18px;
font-weight: bold;
}
.options {
margin-bottom: 20px;
}
.option {
margin: 10px 0;
}
button {
background-color: #4CAF50;
color: white;
padding: 12px 30px;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
}
button:hover {
background-color: #45a049;
}
.hint {
margin-top: 20px;
padding: 15px;
background-color: #e3f2fd;
border-left: 4px solid #2196F3;
font-size: 14px;
}
</style>
</head>
<body>
<div class="quiz-container">
<h1>ランダムクイズに挑戦!</h1>
<form method="post" action="submit.php">
<div class="question">
問題: <?php echo htmlspecialchars($selectedQuestion["question"]); ?>
</div>
<div class="options">
<?php foreach ($selectedQuestion["options"] as $index => $option): ?>
<div class="option">
<label>
<input type="radio" name="answer" value="<?php echo htmlspecialchars($option); ?>" <?php echo $index === 0 ? 'required' : ''; ?>>
<?php echo htmlspecialchars($option); ?>
</label>
</div>
<?php endforeach; ?>
</div>
<!-- 正解をhidden fieldで送信(サーバーで管理) -->
<input type="hidden" name="correct_answer" value="<?php echo htmlspecialchars($selectedQuestion["answer"]); ?>">
<button type="submit">答えを送信</button>
</form>
<div class="hint">
<strong>💡 ポイント:</strong>
このクイズでは、問題がランダムに出題されます。
ブラウザの開発者ツールを開いて、HTMLソースを見てみましょう。
正解が隠されていることが確認できます。
</div>
</div>
</body>
</html>