我有一个由多维数组组成的测验。我希望能够验证单选按钮,以便显示回显的消息,并且用户必须为每个问题单击其中一个单选按钮。我的验证目前尚无效,但我不了解如何在输入中回显单选按钮时验证它们。
<form method="post" action="">
<?php
$quiz = array(
'questions' => array(
"1. How many different stages of belts are there? <br>",
"2. What are the four basic kicks of Tae Kwon Do? <br>",
"3. When was Tae Kwon Do taught in America? <br>",
"4. What does Tae Kwon Do mean? <br>",
"5. Where did Tae Kwon Do originate? <br>",
),
'choices' => array(
array("A. 10", "B. 8", "C. 11 <br>"),
array("A. Ax Kick, Flying Sidekick, Back kick, Front Kick", "B. Front Kick, Side Kick, Back Kick, Roundhouse Kick", "C. Front Kick, Back Kick, Side Kick, Hook Kick <br>"),
array("A. 1963", "B. 1940s", "C. 1980 <br>"),
array("A. The way of the foot and fist", "B. To fight with foot and fist", "C. To fight and defend <br>"),
array("A. South Korea", "B. China", "C. Japan <br>"),
),
'correct' => array("C. 11",
"B. Front Kick, Side Kick, Back Kick, Roundhouse Kick",
"A. 1963",
"A. The way of the foot and fist",
"A. South Korea"),
);//end of $quiz array
//Questions
foreach($quiz['questions'] as $i => $question){
echo $question;
foreach($quiz['choices'][$i] as $j => $choice){
echo "<input type='radio' name='$i'>{$choice}<br>";
}
//Validation
if(isset($_POST['submit'])){
if($_POST[$i] == $quiz['correct'][$i]){
echo 'Please select an answer.';
}
}
}