0

I have problem with my code when radio button isn't selected (on the picture):picture here

This is my code (error is on line 123 and 124)

           <?php 
$Questions = array(
    1 => array(
        'Question' => '1. CSS stands for',
        'Answers' => array(
            'A' => 'Computer Styled Sections',
            'B' => 'Cascading Style Sheets',
            'C' => 'Crazy Solid Shapes'
        ),
        'CorrectAnswer' => 'B'
    ),
    2 => array(
        'Question' => '2. What is the Capital of the Philippines',
        'Answers' => array(
            'A' => 'Cebu City',
            'B' => 'Davao City',
            'C' => 'Manila City'
        ),
        'CorrectAnswer' => 'C'
    )
);

if (isset($_POST['answers'])){
    $Answers = $_POST['answers']; // Get submitted answers.

    // Now this is fun, automated question checking! ;)

    foreach ($Questions as $QuestionNo => $Value){
        // Echo the question
        echo $Value['Question'].'<br />';

        if ($Answers[$QuestionNo] != $Value['CorrectAnswer']){
             echo 'You answered: <span style="color: red;">'.$Value['Answers'][$Answers[$QuestionNo]].'</span><br>'; // Replace style with a class
             echo 'Correct answer: <span style="color: green;">'.$Value['Answers'][$Value['CorrectAnswer']].'</span>';
        } else {
            echo 'Correct answer: <span style="color: green;">'.$Value['Answers'][$Answers[$QuestionNo]].'</span><br>'; // Replace style with a class
            echo 'You are correct: <span style="color: green;">'.$Value['Answers'][$Answers[$QuestionNo]].'</span>'; @$counter++;

        }

        echo '<br /><hr>'; 
                                if (@$counter=="") 
                                { 
                                $counter='0';
                                $results = "Your score: $counter/2"; 
                                }
                                else 
                                { 
                                $results = "Your score: $counter/2"; 
                                }
            }                           echo $results;
} else {  
?>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" id="quiz">
    <?php foreach ($Questions as $QuestionNo => $Value){ ?>

        <h3><?php echo $Value['Question']; ?></h3>
        <?php 
            foreach ($Value['Answers'] as $Letter => $Answer){ 
            $Label = 'question-'.$QuestionNo.'-answers-'.$Letter;
        ?>
        <div>
            <input type="radio" name="answers[<?php echo $QuestionNo; ?>]" id="<?php echo $Label; ?>" value="<?php echo $Letter; ?>" />
            <label for="<?php echo $Label; ?>"><?php echo $Letter; ?>) <?php echo $Answer; ?> </label>
        </div>
        <?php } ?>

    <?php } ?>

The code works, but when no radio button is selected, I get the error in the photo "Warning: Undefined array key ". When the radio buttons are selected, everything works properly.

if ($Answers[$QuestionNo] != $Value['CorrectAnswer']){
             echo 'You answered: <span style="color: red;">'.$Value['Answers'][$Answers[$QuestionNo]].'</span><br>'; // Replace style with a class
             echo 'Correct answer: <span style="color: green;">'.$Value['Answers'][$Value['CorrectAnswer']].'</span>';
        } else {
            echo 'Correct answer: <span style="color: green;">'.$Value['Answers'][$Answers[$QuestionNo]].'</span><br>'; // Replace style with a class
            echo 'You are correct: <span style="color: green;">'.$Value['Answers'][$Answers[$QuestionNo]].'</span>'; @$counter++;

        }

Here are 123 and 124 line.

  • 2
    Does this answer your question? ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – El_Vanja Feb 19 '21 at 14:36
  • 1
    The conclusion to reach here is that only selected radio options are submitted (which is the only thing that makes sense - why would you receive all of them?), thus an umarked radio will not be submitted. You either need to add some validation that prevents this or, if it's a valid case of leaving a question unanswered, you need to check if this index exists before trying to use it. – El_Vanja Feb 19 '21 at 14:36
  • Logically one of a set of radio buttons should always be selected ( I am unfortunately old enough to remember the real buttons on radios) One had to be pressed (defaulted to selected) If you dont want that you should probably be using CheckBoxes. Although the same applies to checkboxes in that if they are NOT checked they do not get sent to the server from a form – RiggsFolly Feb 19 '21 at 14:40
  • Can i use this solution: error_reporting(E_ALL ^ E_NOTICE); ? – Zlatimir Petrov Feb 19 '21 at 14:41
  • Thats like `if (@$counter=="") ` is NOT A SOLUTION!!!!!!! It is just a way of masking your lack of knowledge – RiggsFolly Feb 19 '21 at 14:41
  • consider `isset()` – RiggsFolly Feb 19 '21 at 14:43
  • Well thank you. I will try isset() , I know that @ is not a solution but I'm a beginner and I'm 14 years old. – Zlatimir Petrov Feb 19 '21 at 14:48

0 Answers0