0

I'm trying to create a quiz form with these types of input:

<input type="date" name="Q1"/>
<input type="checkbox" id="2a" name="Q2[]"/>
<input type="checkbox" id="2b" name="Q2[]"/>
<input type="checkbox" id="2c" name="Q2[]"/>
<input type="radio" name="Q3a"/>
<input type="radio" name="Q3b"/>
<input type="radio" name="Q3c"/>
<input type="radio" name="Q4a"/>
<input type="radio" name="Q4b"/>

and in php I want to get those input and check their answers using a for loop:

$answer = array(//a whole bunch of answers);
for ($i=0; $i<6; $i++){
    $response = $_POST["Q".$i];
    $response = sanitise_input($response);
    if (in_array($response, $answer)){
        $point++;
    }
}
echo($point);

The basic concept of this works, but what about Q2, 3 and 4? Q2's got [] with it, and Q3 and Q4's got a, b and c(not in Q4) in the name attribute with them, and I don't know how to code it so that if "Q".$i is not found then try finding "Q".$i."[]", "Q".$i."a" and so on....

Thanks in advance

EDIT: Ok, so as suggested, I've changed my input names into

<input type="date" name="Q1"/>
<input type="checkbox" id="2a" name="Q2[]"/>
<input type="checkbox" id="2b" name="Q2[]"/>
<input type="checkbox" id="2c" name="Q2[]"/>
<input type="radio" name="Q3[a]"/>
<input type="radio" name="Q3[b]"/>
<input type="radio" name="Q3[c]"/>
<input type="radio" name="Q4[a]"/>
<input type="radio" name="Q4[b]"/>

This means I'll need a different way to give these questions a mark, because these are returned in an array, and I don't think in_array will still work with this.

7
  • $aCharCode=97; $varName = "Q".$i.chr($aCharCode+$j) where $j would go from zero to the count of possible letters. See PHP chr function, ord function and this ASCII table asciitable.com Commented May 19, 2018 at 13:39
  • Yeah, but I don't know how to check when $_POST["Q".$i]; returns an "Undefined index" error. If I can then sure I might use that method... Commented May 19, 2018 at 13:49
  • you can add a hidden input with the question ID so in PHP you will know how much inputs to loop. Also check with if ( isset($_POST[$inputName]) ) { } Commented May 19, 2018 at 13:54
  • Why not build your names with more array notation e.g. instead of Q3a do Q[3][a] ? It makes it easier to loop and get data later on. Commented May 19, 2018 at 13:56
  • @TudorIlisoi thanks, that works for me now. Commented May 19, 2018 at 14:08

3 Answers 3

2

I think it's better to change the names.

<input type="date" name="Q1"/>
<input type="checkbox" id="2a" name="Q2[]"/>
<input type="checkbox" id="2b" name="Q2[]"/>
<input type="checkbox" id="2c" name="Q2[]"/>
<input type="radio" name="Q3[a]"/>
<input type="radio" name="Q3[b]"/>
<input type="radio" name="Q3[c]"/>
<input type="radio" name="Q4[a]"/>
<input type="radio" name="Q4[b]"/>
Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

$answer = array(/*a whole bunch of answers*/);
$firstQuestionIndex = 1;
$maxQuestions = 100000;
$step = 1;
for ($i = $firstQuestionIndex; $i < $maxQuestions; $i++) {
    switch($step) {
        case 1: 
            if(isset($_POST["Q".$i]) && !empty($_POST["Q".$i])) {      
                $response = $_POST["Q".$i];
                $response = sanitise_input($response);
                if (in_array($response, $answer)){
                    $point++;
                }
            } else {
                $step++;
                $i = $firstQuestionIndex - 1;
            }
            break;
        case 2: 
            if(isset($_POST["Q".$i.'[]']) && !empty($_POST["Q".$i.'[]'])) {      
                $response = $_POST["Q".$i.'[]'];
                $response = sanitise_input($response);
                if (in_array($response, $answer)){
                    $point++;
                }
            } else {
                $step++;
                $i = $firstQuestionIndex - 1;
            }
            break;
        default:
            for ($j = 0; $j < $maxQuestions; $j++) {
                if(isset($_POST["Q".$i.chr($j+ord('a'))]) && !empty($_POST["Q".$i.chr($j+ord('a'))])) {      
                    $response = $_POST["Q".$i.chr($j+ord('a'))];
                    $response = sanitise_input($response);
                    if (in_array($response, $answer)){
                        $point++;
                    }
                } else {
                    if($j == 0) {
                        $i = $maxQuestions;
                    } else {
                        $i = $firstQuestionIndex - 1;
                    }
                    $j = $maxQuestions;
                }
            }
            break;
    }
}
echo($point);

1 Comment

When you use array notation [] in your input names, it will come back to you as an array. In other words, you will not get back any key with [].
0

Ok I've solved the giving marks to the questions that's got an array as the response using this page

for ($i=1; $i<=4; $i++){
    $response = $_POST["Q".$i];
    if (in_array($response, $answer)){
        $point++;
    } elseif (count(array_diff($_POST["Q".$i], $answer)) == 0){
        $point++;
    }
}   

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.