0

I am trying to create a quiz where the user is answering multiple questions.

I made each question using radios and i am trying to check if that radio is checked. if the radio is not check then i display an error. I mostly got that part working.
My issues is that say if questions 1 has a radio that is checked and question 2 was not answered, when the user hits submit then the answer they put for question 1 is gone.

I want to keep the answer that they checked, like a sticky form, and only display the error on the questions, in this case question 2, that they did not answer. Below is two different ways i tried to solve it and i cant seem to accomplish it.

This is how i am displaying the question with php.

<tr>
    <td> If a and b are negative numbers, and |a| < |b|, then b - a is negative. </td>
    <td> <?php echo ($err['q[1]']? "<span style='color:red'>*".$err['q[1]']."</span><br>": "");?>
        <input type="radio" name="q[1]" id="q[1]t" value="T"  <?php if (isset($_POST['q[1]']) and $_POST['q[1]'] == 'T') { echo 'checked'; } ?> > TRUE
        <input type="radio" name="q[1]" id="q[1]f" value="F"  <?php if (isset($_POST['q[1]']) and $_POST['q[1]'] == 'F') echo 'checked'; ?> > FALSE
    </td>
</tr>
<tr>
    <td> The equation 2x + 7 = 2(x + 5) has one solution. </td>
    <td>    <?php echo ($err['q[2]']? "<span style='color:red'>*".$err['q[2]']."</span><br>": "");?>
        <input type="radio" name="q[2]" id="q[2]t" value="T"  <?php if (isset($_POST['q[2]']) and $_POST['q[2]'] == 'T') echo 'checked'; ?> > TRUE
        <input type="radio" name="q[2]" id="q[2]f" value="F"  <?php if (isset($_POST['q[2]']) and $_POST['q[2]'] == 'F') echo 'checked'; ?> > FALSE
    </td>
</tr>

This is how i am trying to verify it.

if (isset($_POST) && !empty($_POST)) {

    if (isset($_POST['q[1]'])) {
        $radio_input = $_POST['q[1]'];
        echo $radio_input;
        $error=false;
    } else {
        $err['q[1]']= "Please Select An Answer";
        $error=true;
    }
    if (empty($_POST['q[2]'])) {
        $err['q[2]']= "Please Select An Answer";
        $error=true;
    } else {
        $error=false;
    }
m0d3r
  • 107
  • 1
  • 10
  • Are you submitting to the same page OR submitting to another page and redirecting back to the original page? – Mikey Feb 26 '17 at 02:15
  • @Mikey what i am gonna do is keep it on the same page. Then when they answered all the questions im gonna compare it to a text file i have with the answers and then display the table with Question 1 was correct , question 2 was incorrect etc... – m0d3r Feb 26 '17 at 02:24

1 Answers1

1

When you submit a form with inputs using array notation [], they will come back as array in the $_POST. You would access your inputs using e.g. $_POST['q'][1].

Just remember that $err['q[1]'] !== $err['q'][1]

<?php
$err = array();
// check if form was submitted with POST
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // simplified
    for ($i = 1; $i <= 2; $i++) {
        // check if no answer was selected
        if (empty($_POST['q'][$i])) {
            $err["q[$i]"] = "Please Select An Answer";
        }
    }
}
?>

<form action="" method="post">
    <table>
        <tr>
            <td> If a and b are negative numbers, and |a| < |b|, then b - a is negative.</td>
            <td> 
                <?php if (isset($err['q[1]'])) : ?>
                    <span style="color: red">* <?= $err['q[1]'] ?></span><br>
                <?php endif ?>
                <input type="radio" name="q[1]" id="q[1]t" value="T" <?= isset($_POST['q'][1]) && $_POST['q'][1] == 'T' ? 'checked' : '' ?>> TRUE
                <input type="radio" name="q[1]" id="q[1]f" value="F" <?= isset($_POST['q'][1]) && $_POST['q'][1] == 'F' ? 'checked' : '' ?>> FALSE
            </td>
        </tr>
        <tr>
            <td> The equation 2x + 7 = 2(x + 5) has one solution. </td>
            <td>    
                <?php if (isset($err['q[2]'])) : ?>
                    <span style="color: red">* <?= $err['q[2]'] ?></span><br>
                <?php endif ?>
                <input type="radio" name="q[2]" id="q[2]t" value="T" <?= isset($_POST['q'][2]) && $_POST['q'][2] == 'T' ? 'checked' : '' ?>> TRUE
                <input type="radio" name="q[2]" id="q[2]f" value="F" <?= isset($_POST['q'][2]) && $_POST['q'][2] == 'F' ? 'checked' : '' ?>> FALSE
            </td>
        </tr>
        <tr>
            <td></td>
            <td><input type="submit" value="Check"></td>
        </tr>
    </table>
</form>
Mikey
  • 6,728
  • 4
  • 22
  • 45
  • thank you so much. I solved this exactly when you wrote this solution. This is a hundred times better than what my solutions was. I changed my name to different variables on all my questions but this is way more logical. I am a beginner to PHP and what you just taught me will take me very far. Thank you so very much for your help and knowledge. – m0d3r Feb 26 '17 at 03:16
  • 1
    No problem. Since you are using the same HTML structure for each question, you can probably simplify your form some more by storing your questions in an array and looping through them to print each of them. – Mikey Feb 26 '17 at 03:24
  • God bless man! i will do my best to work on that now and get that going! – m0d3r Feb 26 '17 at 03:27