[英]Codeigniter - multiple checkbox values returns NULL
我的視圖中有多個復選框,但是當傳遞給Controller時,它將返回NULL值。
我已經在( 這里 , 這里 和這里以及其他站點)搜索了所有可能的答案,但沒有任何幫助。
這是我的一些CI代碼:
模型(My_Model)
public function get_query_array($query_statement)
{
$query = $this->db->query($query_statement);
return $query->result();
}
控制器(view_leave)
$data["leave_list"] = $this->My_Model->get_query_array("SELECT * FROM Leave_List WHERE Leave_Status='ACTIVE'");
$this->load->view('header_view');
$this->load->view('view_leave', $data);
$this->load->view('footer_view');
PHP / HTML(view_leave.php)
<form role="form" method="post" action="<?php echo base_url();?>index.php/home/update_leave" >
<?php
// assign all leave data into checkboxes and will be submitted to /updated_leave
foreach($leave_list as $row)
{
echo "<input type='checkbox' name='leave_types[]' value='".$row->Leave_Code."'> ".$row->Leave_Description."<br/>";
}
?>
<input type="submit" value="Submit" name="submit_form" id="submit_form">
</form>
控制器(update_leave)
$data = $this->input->post('leave_types');
var_dump($data);
// Here I got some errors when I checked on the boxes
// I use the code above to check if my check boxes are working
// Unfortunately, it returns a NULL value
為什么以及如何檢查復選框值?
請幫忙。
謝謝!
我的樣本計算出來
我的控制器
public function workout()
{
if($this->input->post())
{
echo "<pre>"; print_r($this->input->post()); exit();
}
$this->load->view('workout');
}
我的觀點
<form name="customers" id="customers" method="post" action="<?php echo base_url(); ?>welcome/workout" enctype="multipart/form-data" >
<input type='checkbox' name='leave_types[]' value='1'>1<br/>
<input type='checkbox' name='leave_types[]' value='2'>2<br/>
<input type='checkbox' name='leave_types[]' value='3'>3<br/>
<input type='checkbox' name='leave_types[]' value='4'>4<br/>
<input type='checkbox' name='leave_types[]' value='5'>5<br/>
<input type='submit' name='submit' value='Submit'>Submit<br/>
</form>
當我運行這個時,我的輸出是
Array
(
[leave_types] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[submit] => Submit
)
試試這個希望對您有幫助。 :)
模型(My_Model)
public function get_query_array()
{
$this->db->where('Leave_Status', 'ACTIVE');
$query = $this->db->get('Leave_List');
return $query->result();
}
控制器(View_leave)
$data["leave_list"] = $this->My_Model->get_query_array();
$this->load->view('header_view');
$this->load->view('view_leave', $data);
$this->load->view('footer_view');
PHP / HTML(view_leave.php)
<?php
// assign all leave data into checkboxes and will be submitted to /updated_leave
foreach($leave_list as $row)
{
echo "<input type='checkbox' name='leave_types[]' value='".$row->Leave_Code."' /> ".$row->Leave_Description."<br/>";
}
?>
控制器(update_leave)
$data = $this->input->post('leave_types[]');
var_dump($data);
像這樣更改視圖
<?php $attributes = array('name'=>'leave_form','id'=>'leave_form');
echo form_open('home/update_leave',$attributes);?>
<?php
if(isset($leave_list)){
foreach($leave_list as $row): ?>
<input type='checkbox' name='leave_types[]' value='<?php echo $row->Leave_Code; ?>' /> <?php echo $row->Leave_Description; ?>
<?php endforeach; } ?>
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.