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>
3
  • 1
    try to check this print_r($this->input->post()); Commented Jan 6, 2016 at 23:41
  • 1
    Refer: stackoverflow.com/questions/3112690/… Commented Jan 7, 2016 at 5:09
  • please put view and controller code Commented Jan 7, 2016 at 5:49

4 Answers 4

1

the return post is an array , so try the following

$user_assign = $this->input->post('user_assign');
echo "<pre>";
print_r($user_assign);
Sign up to request clarification or add additional context in comments.

9 Comments

This just echos <pre>
have you checked check box before submitting ?
Yes. and I can see in the post info via Firebug that the correct checkboxes etc are being posted. The data is there, but for some strange reason its not being picked up by the controller.
are you posting it via ajax ? is there any url so that i can view the working ?
Nope, not via Ajax. Yeah its only a demo system, so details ill send
|
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;
}

5 Comments

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 did you try foreach one??
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
var_dump($_POST('user_assign')) and post answer
Fatal error: Array callback has to contain indices 0 and 1 in /home/enets/public_html/system/application/controllers/assignments.php on line 68
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...!

10 Comments

Tried this as you can see from the commented code in my controller. Array/object still empty?
OK, i tried the above code, the output was: Array ( [user_assign] => Array ( [0] => 100002 [1] => 100003 ) [course_name] => Slips & Trips )
I can echo/print the POST vars, i just cant get the array into the controller to use
Why cant i get the POST vars into the controller with $this->input->post('user_assign'). it just makes no sense whatsoever??????
|
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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.