brunobliss Posted December 7, 2013 Share Posted December 7, 2013 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>");?> Quote Link to comment https://forums.phpfreaks.com/topic/284613-form-validation-with-question-problem/ Share on other sites More sharing options...
denno020 Posted December 7, 2013 Share Posted December 7, 2013 (edited) The reason is when you submit the form, $arrayIndex is populated with another random answer, because the script is run again from the start. What you need to do is store the answer some other way, and check against that. You can used a session variable for that. Also, move the answer check to the beginning of the script, so the first thing that it does is check for a submit. See the following: if (isset($_POST['submit'])) { if ($_POST['answer'] == $_SESSION['answer']) { echo "correct"; } else { echo "incorrect"; } } else { echo "Answer this:"; } $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']; $_SESSION['answer'] = $a; 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> "); Denno Edited December 7, 2013 by denno020 Quote Link to comment https://forums.phpfreaks.com/topic/284613-form-validation-with-question-problem/#findComment-1461611 Share on other sites More sharing options...
Ch0cu3r Posted December 8, 2013 Share Posted December 8, 2013 (edited) If you use Denno's code above make sure to call session_start for the sessions to work Edited December 8, 2013 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/284613-form-validation-with-question-problem/#findComment-1461639 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.