0

Im using codeigniter and I have a very simple form, which has a few checkboxes:

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

Now, the values are definately getting posted because I can see them through firebug.

user_assign[]100002
user_assign[]100003
course_name Asbestos

But the below is empty? and i just dont understand?

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

How is this possible?

Controller (which is damn messy because I've tried 1001 things)

    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();      


}   

View (which is very basic because I've stripped everything down in order to try and get these values through)

                <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>  

The form that contains the checkboxes

                        <?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>
frobak
  • 557
  • 2
  • 11
  • 30

4 Answers4

1

the return post is an array , so try the following

$user_assign = $this->input->post('user_assign');
echo "<pre>";
print_r($user_assign);
krishnakumar kk
  • 289
  • 2
  • 5
1

Try this

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

In Controller

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

or

foreach ($_POST('user_assign') as $item) {
    echo $item;
}
Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
  • This give me the following error: Fatal error: Array callback has to contain indices 0 and 1 in /home/enets/public_html/system/application/controllers/assignments.php on line 80 – frobak Jan 07 '16 at 09:56
  • @frobak did you try foreach one?? – Abdulla Nilam Jan 07 '16 at 09:57
  • Yes, tried both, both give same error: Fatal error: Array callback has to contain indices 0 and 1 in /home/enets/public_html/system/application/controllers/assignments.php on line 68 – frobak Jan 07 '16 at 10:00
  • `var_dump($_POST('user_assign'))` and post answer – Abdulla Nilam Jan 07 '16 at 10:02
  • Fatal error: Array callback has to contain indices 0 and 1 in /home/enets/public_html/system/application/controllers/assignments.php on line 68 – frobak Jan 07 '16 at 10:04
1

Also check form's action as 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();      


}   

Try this...!

Giri Annamalai M
  • 810
  • 9
  • 24
0

OK, so i managed to get it working via

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

Don't ask me how or why?

Thanks everyone for your efforts in trying to solve this!

frobak
  • 557
  • 2
  • 11
  • 30