php - How to tick any given check box inside a form? -


for ($i=0; $i<$total_questions_for_exam; $i++) {           $question_id= $question_and_answers[$i]['question_id'];         $numberofanswersperquestion = count_answers_belongtoone_questionnew($question_id);         //die(var_dump($question_id));          $student_answer_per_question = retrieve_student_result ($_session['user_id'], $_get['quiz_id'], $question_id);         $correct_answer = $numberofanswersperquestion[0]['answer_name'];           echo ' <form method="post" id="review_form" name="review_form" action="view_user_summary.php">                <table class="table table-bordered table-condensed table-datatable table-hover review_marks">                 <tbody>                      <tr>                         <td style="text-align: left;" width="100%"><strong>question '. ($i+1) .'</strong></td>                      </tr>                     <tr>                         <td style="text-align: left;" width="100%">' . $question_and_answers[$i]['question_name'] .'</td>                     </tr>';                      if($student_answer_per_question == '')                        echo  '<tr>                                 <td style="text-align: left;" width="100%" class="warning"><em>question not attempted</em><br><strong>correct answer </strong><br>' . $numberofanswersperquestion[0]['answer_name'] . '</td>                             </tr>';                      else if ($student_answer_per_question == $correct_answer)                       echo  '<tr>                                 <td style="text-align: left;" width="100%" class="success"><strong>your answer correct.</strong><br>' . $student_answer_per_question .'</td>                             </tr>';                      else                          echo    '<tr>                                     <td style="text-align: left;" width="100%" class="danger">                                         <table style="width: 100%">                                             <tbody>                                                 <tr>                                                     <th style="width: 50%"><strong>your answer</strong></th>                                                     <th style="width: 50%"><strong>correct answer</strong></th>                                                 </tr>                                                 <tr>                                                     <td style="width: 50%">' . $student_answer_per_question . '</td>                                                     <td style="width: 50%">' . $correct_answer .'</td>                                                 </tr>                                             </tbody>                                         </table>                                     </td>                                 </tr>';                      echo                       '<tr>                         <td style="height: 5px;" width="100%">&nbsp;</td>                     </tr>                      <tr>                     <td style="height: 5px;" width="100%">&nbsp;</td>                     </tr>                  </tbody>             </table>              <div class="[ form-group ] correct_answer">                 <input type="checkbox" name="fancy-checkbox-primary"  id="fancy-checkbox-primary" autocomplete="off" />                 <div class="[ btn-group ]">                     <label for="fancy-checkbox-primary" class="[ btn btn-primary ]">                         <span class="[ glyphicon glyphicon-ok ]"></span>                         <span> </span>                     </label>                     <label for="fancy-checkbox-primary" class="[ btn btn-default active ]">                         correct answer                     </label>                 </div>             </div>';  }  echo '<button class="btn btn-large btn-block btn-primary submitbutton" type="submit" style="margin-top:100px;" name="submit_review">submit review</button></form>'; 

hi guys code above pulls question , asnwers in database. trying achieve have check box within loop allows users tick box answer correct, or if don't assume answer wrong.

screenshot of how looks

it display how wanted 1 problem, when click on 2nd check box, first 1 still ticking, not have control of 2nd check box. idea how can go fixing ?

thought idea show video of meant, have provided link it.

problem

you have in loop:

<input type="checkbox" name="fancy-checkbox-primary" id="fancy-checkbox-primary" autocomplete="off" /> 

which means checkboxes have same id. that's invalid markup, , behavior of code attempts use id going undefined. (it might find first matching element, last matching element, matching elements, no matching elements, etc.)

an id, name implies, meant identify element. need correct markup elements can identified, adjust code uses elements identify them.

this involve appending value id in loop, making each 1 unique. or perhaps using class instead of id this. code in question can't speak latter, former should simple enough:

<input type="checkbox" name="fancy-checkbox-primary" id="fancy-checkbox-primary-' . $i . '" autocomplete="off" /> 

(note: i'm not entirely familiar mechanism you're using style these checkboxes, there's plugin @ play here. it's possible may need modify name and/or for attributes associated id value well. in code, shown in question or otherwise, relies on id.)

though it's still not 100% clear implications of code not included in question, may need tinker own needs. fundamentally issue here repeated id values.


Comments