Jump to content

form validation with question problem


brunobliss

Recommended Posts

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>
");

?>

Link to comment
Share on other sites

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 by denno020
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.