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.

Khang
  • 141
  • 1
  • 11
  • $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 http://www.asciitable.com/ – Tudor Ilisoi May 19 '18 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... – Khang May 19 '18 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]) ) { }` – Tudor Ilisoi May 19 '18 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. – Mikey May 19 '18 at 13:56
  • @TudorIlisoi thanks, that works for me now. – Khang May 19 '18 at 14:08
  • @Mikey, won't the name attribute quote the entire thing into literally "Q[3][a]"? – Khang May 19 '18 at 14:08
  • Nope. It will come back to you as a multi-dimensional array. Check by doing `echo '
    '; print_r($_POST['Q']);`
    – Mikey May 19 '18 at 14:09

3 Answers3

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]"/>
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);
le Mandarin
  • 192
  • 10
  • When you use array notation `[]` in your input names, it will come back to you as [an array](https://stackoverflow.com/questions/20184670/html-php-form-input-as-array). In other words, you will not get back any key with `[]`. – Mikey May 19 '18 at 14:21
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++;
    }
}   
Khang
  • 141
  • 1
  • 11