Codeigniter多个复选框问题

时间:2016-01-06 21:48:02

标签: php codeigniter

我使用codeigniter,我有一个非常简单的表单,它有几个复选框:

<input type="checkbox" name="user_assign[]" value="' . $row->user_id . '">

现在,这些值肯定会被发布,因为我可以通过萤火虫看到它们。

user_assign[]100002
user_assign[]100003
course_name Asbestos

但是下面是空的?我只是不明白?

$user_assign = $this->input->post('user_assign');

这怎么可能?

控制器(因为我尝试了1001件事,这本来就太乱了)

    function update_course_assignment_org() {

    $data = $this->input->post('user_assign');

    $this->load->model('courses_model');
    $this->load->model('assignment_model');

    // get course id from posted course name
    $course_name = $this->input->post('course_name');
    $course_object = $this->courses_model->get_course_id($course_name);
    $course_id = $course_object[0]->course_id;

    //$user_assign = substr(implode(', ', $this->input->post('user_assign')), 0);


    //foreach ($this->input->post('user_assign') as $key => $value) {
        $test_obj = array(
           'course' => $data,
           'users' => $course_id,
           'org' => $this->input->post('course_name')
        ); 
    //}     

    //$this->assignment_model->save_org_assignments($user_ids);

    $this->template->write('title', 'Assignments');
    $this->template->write('navigation_strip', $this->load->view('navigation', array(), TRUE));         
    $this->template->write('content', $this->load->view('/assignments/assign_test', $test_obj, TRUE));

    $this->template->render();      


}   

查看(这是非常基本的,因为我已经删除所有内容以尝试获取这些值)

                <div class="row">
                <div class="col-md-4">
                    <?php var_dump($course); print_r($course); ?>
                </div>
                <div class="col-md-4">
                <?php print_r($users); ?>
                <?php print_r($org); ?>
                </div>                  
            </div>  

包含复选框的表单

                        <?php echo form_open_multipart('assignments/update_course_assignment_org');?>
                        <table>
                            <tr>
                                <th>User</th>
                                <th>Assign course</th>
                            </tr>
                            <?php
                                foreach($users as $row) {                           

                                    echo '<tr>';
                                        echo '<td>' . $row->fname . ' ' . $row->sname . '</td>'; 
                                        echo '<td><input type="checkbox" name="user_assign[]" value="' . $row->user_id . '"></td>'; 
                                    echo '</tr>';

                                }
                            ?>
                            <tr>
                                <td>
                                    <input type="hidden" name="course_name" value="<?php echo $course[0]->course_name; ?>">                 
                                    <input type="submit" value="Assign" />
                                </td>
                            </tr>                               
                        </table>
                    </form>

4 个答案:

答案 0 :(得分:1)

返回帖是一个数组,请尝试以下

$user_assign = $this->input->post('user_assign');
echo "<pre>";
print_r($user_assign);

答案 1 :(得分:1)

试试这个

<input type="checkbox" name="user_assign[]" value="<?php echo $row['user_id'] ?>">

在控制器中

$user_assign[] = $_POST('user_assign');
print_r($user_assign);

foreach ($_POST('user_assign') as $item) {
    echo $item;
}

答案 2 :(得分:1)

同时将表单的操作检查为action ='controller / update_course_assignment_org'

function update_course_assignment_org() {

   $this->form_validation->set_rules('user_assign','Check now','required');

   if($this->form_validation->run()){
     $data = $user_assign = implode(',',$this->input->post('user_assign') );
     print_r($_POST);
     exit;
  }
    $this->load->model('courses_model');
    $this->load->model('assignment_model');

    // get course id from posted course name
    $course_name = $this->input->post('course_name');
    $course_object = $this->courses_model->get_course_id($course_name);
    $course_id = $course_object[0]->course_id;

    //$user_assign = substr(implode(', ', $this->input->post('user_assign')), 0);


    //foreach ($this->input->post('user_assign') as $key => $value) {
        $test_obj = array(
           'course' => $data,
           'users' => $course_id,
           'org' => $this->input->post('course_name')
        ); 
    //}     

    //$this->assignment_model->save_org_assignments($user_ids);

    $this->template->write('title', 'Assignments');
    $this->template->write('navigation_strip', $this->load->view('navigation', array(), TRUE));         
    $this->template->write('content', $this->load->view('/assignments/assign_test', $test_obj, TRUE));

    $this->template->render();      


}   

试试这个......!

答案 3 :(得分:0)

好的,所以我设法让它通过

工作
$assignmen = @$_POST['user_assign'];

不要问我怎么或为什么?

感谢大家努力解决这个问题!