如何在Codeigniter中获取选中的复选框值?

时间:2019-01-28 10:42:43

标签: php codeigniter

如果要选中此复选框,我想得到该复选框的值

这是我的代码:

<?php
      foreach ($dosage_form_list as $val) {
 ?>  

    <input type="checkbox" name="dosage_form_input[]" value="<?php echo $val['dosage_form']?>">

    <?php echo $val['dosage_form'];?>

    <?php

       }

     ?> 

2 个答案:

答案 0 :(得分:1)

如果需要,只需附加checked=checked。.

<input type="checkbox" name="dosage_form_input[]" value="<?php echo $val['dosage_form']?>" checked="<?= $someConditional ? 'checked' : ''; ?>">

答案 1 :(得分:0)

我有解决方法:

   <?php
$dosage_form_list_exist = preg_replace('/\s*,\s*/', ',', 
$dosage_form_list_exist); // It removes spaces after the comma only not 
 between the values like "Oral Solutions" etc
// $dosage_form_list_exist = str_replace(' ', '', $dosage_form_list_exist); 
// It removes all the spaces from the Variable
$dosage_list = explode(',',$dosage_form_list_exist);
foreach ($dosage_form_list as $val) {
?>  

<input type="checkbox" name="dosage_form_input[]" value="<?php echo 
$val['dosage_form']?>" <?php echo (in_array($val['dosage_form'],$dosage_list) 
? 'checked' : ''); ?>>

<?php echo $val['dosage_form'];?>

<?php
  }
?>