Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey folks
I'm working on an exam app and I wish to display the results for someone who took an exam. I wish to display the number for the right responses, the wrong ones and unanswered ones.
When I take an exam and submit the answers, I get the error " Undefined variable: value in..."
My issue is getting that name attribute variable with $_POST on my server page for processing needs.Thanks in advance for your help!

What I have tried:

This is what I already have :
HTML Quiz
There are multiple questions, but I'll supply the code for one only
HTML
<div id='question<?php echo $i; ?>' class='cont'>
    <div class="question_css">
        <h3><span class="label label-warning" id="qid"><?php echo  $i; ?></span>
            <span class="questions" id="qname<?php echo $i; ?>"> <?php echo $value['question_entitle']; ?>
        </h3>
    </div>

    <ul class="quiz-ul">

        <li class="quiz-li">
            <input type="radio" value="<?php echo $options[0]['Options'];?>"
                    id='radio1_<?php echo $options[0]['answer_proposal_id']; ?>' name='<?php echo $value['Questionid'];?>'/>
            <label for="radio1_<?php echo $options[0]['answer_proposal_id']; ?>"
                    class="element-animation"><?php echo $options[0]['Options']; ?></label>
            <div class="check"></div>
        </li>

        <li class="quiz-li">
            <input type="radio" value="<?php echo $options[1]['Options'];?>"
                    id='radio2_<?php echo $options[1]['answer_proposal_id']; ?>' name='<?php echo $value['Questionid'];?>'/>
            <label for="radio1_<?php echo $options[1]['answer_proposal_id']; ?>"
                    class="element-animation"><?php echo $options[1]['Options']; ?></label>
            <div class="check"></div>
        </li>

        <li class="quiz-li">
            <input type="radio" value="<?php echo $options[2]['Options'];?>"
                    id='radio3_<?php echo $options[2]['answer_proposal_id']; ?>' name='<?php echo $value['Questionid'];?>'/>
            <label for="radio1_<?php echo $options[2]['answer_proposal_id']; ?>"
                    class="element-animation"><?php echo $options[2]['Options']; ?></label>
            <div class="check"></div>
        </li>

        <li class="quiz-li">
            <input type="radio" value="<?php echo $options[3]['Options'];?>"
                    id='radio4_<?php echo $options[3]['answer_proposal_id']; ?>' name='<?php echo $value['Questionid'];?>'/>
            <label for="radio1_<?php echo $options[3]['answer_proposal_id']; ?>"
                    class="element-animation"><?php echo $options[3]['Options']; ?></label>
            <div class="check"></div>
        </li>
        <li class="quiz-li">
            <input type="radio" checked='checked' style='display:none' value="5"
                id='radio5' name='<?php echo $value['Questionid'];?>'/>
        </li>
    </ul>

    <button id='<?php echo $i; ?>' class='next btn btn-success' type='button'>Suivant</button>
</div>

PHP code
PHP
if (isset($_POST['submit']) && !empty($_POST)) {
    
  $right_answer=0;
  $wrong_answer=0;
  $unanswered=0;
  
  $question_correct_answerid =$db->prepare ('SELECT Questionid, question_correct_answerid from question ');
  $question_correct_answerid->execute();
  $correctAnswerId = $question_correct_answerid->fetchAll();
  
  foreach ($correctAnswerId as $key => $row) {
    $correctOption= $db->prepare('SELECT Options,answer_proposal_id from answerproposal where is_Correct= 1 
                                  and answer_proposal_id = '.$row['question_correct_answerid'].' ');
    $correctOption->execute();
    $correctOptions = $correctOption->fetchAll();
    //var_dump($correctOptions);
  }
  
  
  foreach ($correctOptions as $key => $option) {
    if ($option['Options'] == $_POST[$value['Questionid']]) {
      
      $right_answer++;
    }else if($_POST[$value['Questionid']] == 5){
      $unanswered++;
    }else{
      $wrong_answer++;
    }
  }
}
?>

<div class="container result vh-center">
<div class="w100">
    <hr>   
    <div class="row dflex results-container">    
        <div class="results right">
            <div>
                <p class="mtb-1">Total no. of right answers : 
                  <span class="answer"><?php echo $right_answer;?></span></p>
                <p class="mtb-1">Total no. of wrong answers : 
                  <span class="answer"><?php echo $wrong_answer;?></span></p>
                <p class="mtb-1">Total no. of Unanswered Questions : 
                  <span class="answer"><?php echo $unanswered; ?></span></p>                   
            </div>
        </div>
    </div>    
</div>
</div>
Posted
Updated 16-May-20 11:52am
v2
Comments
Richard MacCutchan 16-May-20 11:55am    
I get the error " Undefined variable: value in..."
OK, you have not declared the variable value anywhere.
Nice Kloe 16-May-20 12:16pm    
I didn't do it because it should come from the quiz-page, where I take the exam and send candidate's chosen answers to the processing page. The attribute name for the radio buttons are name='$value['Questionid'];' and on my server page, I try getting the name attribute using $_POST[$value['Questionid']]. Is this wrong, please?
Richard MacCutchan 16-May-20 12:57pm    
You cannot use a variable that has never been declared. Check the PHP language reference for a detailed explanation of variables, references and scope rules.
Nice Kloe 16-May-20 13:17pm    
Ohhhh, ok, I understand. So please, any idea/suggestion/working code snippet on how I can get the different chosen candidate's answers to compare them with the correct answers from my DB?
Richard MacCutchan 17-May-20 4:06am    
Yes, declare a proper variable to capture the data as it is entered and pass that to the backend code which compares it to the values in the database.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900