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.