I have tried this in every way i can think of, there's a multidimensional array with 3 sets of data, each set has both a question and the corresponding awswer, i want to validate the user's answer to each question.
The problem is, when i press the submit button, the user is actually submiting the answer to the next question, which cannot be shown until the submit is pressed! This can be verified by entering an expected value like "2" and wait for the next question to be 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> "); ?>