ivva
ivva

Reputation: 2949

How to set checked for all selected checkboxes

I have many checkboxes. I want when user clicks submit button and he hasn't filled all required fields, these checkboxes that he has checked, to be checked. My code is the following, but when form submit, only last checkbox is cheked.

 function teachers_show(yes, no){
        $(".toggle, .all_teachers_show,  .student, .school,  .teacher_school, .teacher, .class, .teacher_class").hide();
          if (no)
              $('.all_teachers_show').show();
          else
              $('.all_teachers_show').hide();
          $(":radio").prop('checked',false);
          $(yes).prop('checked',true);
      }
<?php
  
  echo validation_errors();
  echo "<div class='container' id='register_container'>";
  echo form_open('home/register');
echo "<table border = '0' >";
  echo "<tr><td><label>  Username:* </label></td><td>";
  $data=array(
    'name' => 'username',
    'class' => form_error('username') ? 'error' : '',
    'value' => set_value('username')
  );
  echo form_input($data);
  echo "</td></tr>";
echo "<tr><td><label> Password:* </label></td><td>";
  $data=array(
    'name' => 'password',
    'class' => form_error('password') ? 'error' : ''
  );
  echo form_password($data);
echo "<tr><td><label>  Choose role:* </label> </td><td>";
$selected_role = $this->input->post('role_id');  ?>
  <input type="radio" name="role_id" id="radio1" onclick="showHide(this, true)" value="1" 
  <?php echo '1' ==  $selected_role ? 'checked="checked"' : 
      '' ?>/>
      <?php  echo " Role 1"; ?>
      <input type="radio" name="role_id" id="radio2" onclick="showHide(this, true)" value="2" 
  <?php echo '2' ==  $selected_role ? 'checked="checked"' : 
      '' ?>/>
       <?php  echo " Role 2 "; ?>
      <input type="radio" name="role_id" id="radio5" onclick="teachers_show(this, true)" value="5" 
  <?php echo '5' ==  $selected_role ? 'checked="checked"' : 
      '' ?>/>
  <?php  echo " Role 3 "; 

  echo "</td></tr>";
 <?php
  echo "<tr class='all_teachers_show' style='display:none;'><td><label>  Teachers:*  </label></td><td>";
  ?>
 <table border='0'>
  <tr>
  <?php    
           $ind = 0;

           foreach ($all_teachers_show as $row) { 
           $ind++; 
?>
  <td>
  <?php $selected_teachers = $this->input->post('all_teachers_show[]');  echo $selected_teachers; ?>
  <input type="checkbox" id='all_teachers_show' <?php echo set_checkbox('all_teachers_show',$row->user_id); ?> name="all_teachers_show[]"
   value="<?= $row->user_id ?>" <?php if ( isset($selected_teachers[$row->user_id] )) 
    echo 'checked="checked"'; ?>><?= $row->first_name . ' ' . $row->last_name ?> <td>
      <?php 
      if($ind % 3 == 0)
       echo '</tr> <tr>';
} ?>
</table>
   <?php
   echo "</td></tr>";
  echo "</div>";
  echo "</table><br/>";
$data=array(
    "name" => 'mysubmit',
    'class' => 'btn btn-success ',
    'id' => 'reg',
    'value' => 'Register'
    
  );
  echo form_submit($data);
  ?>
  </form>

That's my whole code. These chechboxes are for Role 3 - radio button with id='radio5'.

How could it be my if: <?php echo $selected_teachers == $row->user_id ? 'checked="checked"' : '' ?>
$selected_teachers - it returns array and I compare it with $row->user_id How could be done that?

Upvotes: 0

Views: 252

Answers (1)

Captain Crunch
Captain Crunch

Reputation: 587

try this:

<?php 
foreach ($all_teachers_show as $row)
{ 

    $userId = $row->user_id;
    $first_name = $row->first_name;
    $last_name = $row->last_name;
    $teacherSelected = ( isset($_POST[$userId]) ) ? true : false;

    ?>

        <td>
          <input 
                type="checkbox"
                class='all_teachers_show'  
                name="<?php echo $userId; ?>"
                <?php 
                    if ( $teacherSelected ) 
                        echo 'checked="checked"'; 
                ?>
                value="<?php echo $userId; ?>" 
            >
                <?php echo $first_name . ' ' . $last_name ?> 
        <td>
<?php
    }

Upvotes: 1

Related Questions