我想保存我选择的所有复选框值

时间:2016-11-29 06:27:24

标签: php mysql codeigniter

我试图存储复选框值subject_id但是当我将此值存储到数据库时,它只是将最后选中的复选框存储在数据库中...请帮我整理代码,以便我可以获得所有选定的值。

我想保存我选中的所有复选框值,但是如果我检查了多个复选框,它总是会保存最后一个复选框。

 function get_class_section($day='',$section='',$class='')
{

    print_r($section);
    $sections = $this->db->get_where('class_routine' , array(
        'day' => $day,'section_id'=>$section,'class_id'=>$class
    ))->result_array();


    foreach ($sections as $row) {


    echo "<table class=\"table table-bordered datatable\" id=\"table_export\"  >";

    echo "<thead>";
    echo "<th>Subject</th><th colspan=\"2\">status</th>";
    echo "</thead>";

    echo "<tbody>";
    echo "<tr>";

    echo "<td>". $this->crud_model->get_subject_name_by_id($row['subject_id'],"<input type=\"text\"  name=\"subject_id\" value=\"subject_id\" >");
    echo "<input type=\"checkbox\" name=\"types[]\"  value=\"".$row['subject_id']."\" set_checkbox('types[]', 'subject_id')>";

      echo "</td>";

    echo "<td>present&nbsp;&nbsp;<input type=\"checkbox\"  value=\"1\" ></td>"
      . "<td>Absent&nbsp;&nbsp;<input type=\"checkbox\" name=\"types[]\" value=\"0\"></td>";



    echo "</tr>";
    echo "</tbody>";

    echo "</table>"; 




    }

}

如何保存我选中的所有复选框

2 个答案:

答案 0 :(得分:1)

您需要 foreach循环才能获得所有选中的复选框

试试这个:

if(isset($_REQUEST['types']) && $_REQUEST['types']!=array())
{
  foreach ($_REQUEST['types'] as $key => $value) {

   // in $value,you will get checkbox value

  }
}

答案 1 :(得分:0)

您应该对所有复选框使用隐藏元素

试试这个

 . "<td>Absent&nbsp;&nbsp;<input type=\"checkbox\" name=\"types[]\" value=\"0\"></td>
 <input type=\"hidden\" name=\"types[]\" value=\"0\"> ";
相关问题