Working on a quiz website where when users submits their answers $perQuestionScore equals true when the answer is correct but false when it is wrong. I’m trying to find the instances where $perQuestionScore equals true in other to total the score but it doesn’t seem to work.My code looks like below.
<?php
$perQuestionScore = 0;
$perQuestionScore= false;
if (isset($_POST['grader'])) {
if(isset($_POST[$chosen]))
{
$choice= $_POST[$chosen];
if (strpos($choice, $correctOne) !== false) {
$perQuestionScore = true;
echo $_POST[$chosen] . "" . " is the correct answer";
} elseif (strpos($choice, $correctOne) == false) { echo $_POST[$chosen] . "" . " is the Wrong answer";
} else {
echo "You did not choose an answer"; {
}
}
}
}
}
echo "<input id=grader' type='submit' name='grader' value='Grade Quiz'>" . "</form>";
echo $perQuestionScore * 10;
} ?>
Try giving the boolean and the numeric diferent names.
And I’m a bit surprised you aren’t getting error messages if that’s all the code.
The “if” curly braces look off.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 'true');
$perQuestionScore = 0;
$perQuestionScore= false;
if (isset($_POST['grader'])) {
if(isset($_POST[$chosen])) {
$choice= $_POST[$chosen];
if (strpos($choice, $correctOne) !== false) {
$perQuestionScore = true;
echo $_POST[$chosen] . "" . " is the correct answer";
} elseif (strpos($choice, $correctOne) == false) {
echo $_POST[$chosen] . "" . " is the Wrong answer";
} else {
echo "You did not choose an answer";
{
}
}
}
}
}
echo "<input id=grader' type='submit' name='grader' value='Grade Quiz'>" . "</form>";
echo $perQuestionScore * 10;
}
?>
Can you supply or show the input quiz source page. It should then be possible to create a simple demo and hard-code the database test results in arrays.