2015-05-11 63 views
0

我有很多複選框。我希望當用戶點擊提交按鈕時,他沒有填寫所有必填字段,這些複選框他已檢查,以檢查。我的代碼如下,但表單提交時,只有最後一個複選框被拒絕。如何爲所有選中的複選框進行檢查

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>

這就是我的全部代碼。這些chechboxes是角色3 - 單選按鈕id ='radio5'。

怎麼會是我的,如果:<?php echo $selected_teachers == $row->user_id ? 'checked="checked"' : '' ?>
$selected_teachers - 返回數組和我一起$row->user_id 如何能夠做到這一點比較呢?

回答

1

試試這個:

<?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 
    } 
+0

我想,如果我檢查複選框的例子10和我按下提交按鈕 - 這是註冊表單,如果我沒有填寫用戶名,密碼,不要有再次檢查複選框 - 他們會被檢查 –

+0

你可以發佈你的整個代碼嗎? –

+0

是的,我會編輯我的問題。 –

相關問題