0
我試過這種方式,我可以想到,有一個多維數組與3組數據,每組都有一個問題和相應的awswer,我想驗證用戶對每個問題的回答。PHP表單驗證(有點像驗證碼)
問題是,當我按下提交按鈕,用戶實際上是提交下一個問題的答案,直到提交按下才能顯示!這可以通過輸入如「2」的預期值進行驗證,並等待下一個問題是1 + 1 =
<?php
$question = array(
0 => array(
'question' => "1+1=",
'answer' => 2
),
1 => array(
'question' => "2+1=",
'answer' => 3
),
2 => array(
'question' => "4+1=",
'answer' => 5
)
);
$arrayIndex = array_rand($question);
$q = $question[$arrayIndex]['question'];
$a = $question[$arrayIndex]['answer'];
if (isset($_POST['submit'])) {
if($_POST['answer'] == $a) {
echo "correct";
} else {
echo "incorrect";
}
} else {
echo "Answer this:";
}
print $a;
print ("
<form method='post'><br/>
<input type='text name='". $a ."' value='". $q ."'>
<input type='text' name='answer'><br/>
<input type='submit' name='submit'><br/>
</form>
");
?>