如何设置选中所有选中的复选框

时间:2021-10-13 09:14:10

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'.

这是我的整个代码。这些chechboxes用于角色3 - 带有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?

怎么可能是我的if: user_id? 'checked =“checked”':''?> $ selected_teachers - 它返回数组并将其与$ row-> user_id进行比较怎么做?

1 个解决方案

#1


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
    }

#1


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
    }