1

I've the problem to post my checkbox to database. Please help me...

Here is my view :

<table>
    <tr>
        <td>
            <input type='hidden' name='userid[]' value='1'>
            <input type='text' name='username[]' value='username1'> 
        </td>
        <td>
            <input type='checkbox' name='as_admin[]' value=1>
        </td>
    </tr>
    <tr>
        <td>
            <input type='hidden' name='userid[]' value='2'>
            <input type='text' name='username[]' value='username2'> 
        </td>
        <td>
            <input type='checkbox' name='as_admin[]' value=1>
        </td>
    </tr>
</table>

This is my controller :

$this->Model_user->insert_user();

And this is my model :

function insert_user(){
    $user_count = count($this->input->post('userid'));
    $userid     = $this->input->post('userid');
    $username   = $this->input->post('username');
    $as_admin   = $this->input->post('as_admin');

    for ($i=0; $i < $user_count; $i++){
        $info_user = array(
            'user_id'   => $userid[$i],
            'user_name' => $username[$i],
            'as_admin'  => $as_admin[$i],
        );
        $this->db->insert($info_user);
    }
}

And the problem is when 'username2' mark as admin (second row checkbox checked), in the database will be like this :

|user_id|user_name|as_admin|
|   1   |username1|   1    |
|   2   |username2|   0    |

it should be like this :

|user_id|user_name|as_admin|
|   1   |username1|   0    |
|   2   |username2|   1    |

Does anyone now how to save those thing, Please...

Thanks in advance...

5
  • Are you checking both checkbox at the same time? Commented Aug 28, 2016 at 15:45
  • @d.coder Look like it is, use radio instead. Commented Aug 28, 2016 at 15:53
  • when I checked the second row, the checkbox value saved into first row Commented Aug 28, 2016 at 15:56
  • If not then you must use radio. Commented Aug 28, 2016 at 15:56
  • can not use radio, because both can be as admin Commented Aug 28, 2016 at 16:08

2 Answers 2

6

Try this, in your view, use the user id as key for as_admin

<table>
    <tr>
        <td>
            <input type='hidden' name='userid[]' value='1'>
            <input type='text' name='username[]' value='username1'> 
        </td>
        <td>
            <input type='checkbox' name='as_admin[1]' value=1>
        </td>
    </tr>
    <tr>
        <td>
            <input type='hidden' name='userid[]' value='2'>
            <input type='text' name='username[]' value='username2'> 
        </td>
        <td>
            <input type='checkbox' name='as_admin[2]' value=1>
        </td>
    </tr>
</table>

in your model

function insert_user(){
    $user_count = count($this->input->post('userid'));
    $userid     = $this->input->post('userid');
    $username   = $this->input->post('username');
    $as_admin   = $this->input->post('as_admin');

    for ($i=0; $i < $user_count; $i++){
        $info_user = array(
            'user_id'   => $userid[$i],
            'user_name' => $username[$i],
            'as_admin'  => isset($as_admin[$userid[$i]]) ? 1 : 0,
        );
        $this->db->insert($info_user);
    }
}

You are having this problem because, want you check only 1 checkbox, only checked value will be passed, so if you count the $as_admin you will get only 1 out of 2 as_admin.

if you make admin the username2 only

then your post will be as below

Array
(
    [userid] => Array
        (
            [0] => 1
            [1] => 2
        )

    [username] => Array
        (
            [0] => username1
            [1] => username2
        )

    [as_admin] => Array
        (
            [2] => 1
        )
)
Sign up to request clarification or add additional context in comments.

3 Comments

It is not required to set indexes in checkbox name as_admin[1]. But, you have done so then are you sure your for loop will set as_admin properly? isset($as_admin[$userid[$i]]) ? 1 : 0 Loop iterate from 0 to less than $user_count but as_admin indexes are not same.
I have update my answer you will get your answer from that. we are using the user id as key for as_admin
Ohh I completely missed that.
1

The problem is that unchecked checkboxes are not posted and it is possible that none of the boxes are checked. If something is checked you need a way to associate the checkbox with the userid. This answer requires the value of userid[] to be the value of the checkbox. So the html is changed to this.

<table>
  <tr>
    <td>
      <input type='hidden' name='userid[]' value='1'>
      <input type='text' name='username[]' value='username1'> 
    </td>
    <td>
      <input type='checkbox' name='as_admin[]' value=1>
    </td>
  </tr>
  <tr>
    <td>
      <input type='hidden' name='userid[]' value='2'>
      <input type='text' name='username[]' value='username2'> 
    </td>
    <td>
      <input type='checkbox' name='as_admin[]' value=2>
    </td>
  </tr>
</table>

Essentially, the controller function will set $info_user['as_admin'] = 1 if a 'userid' is a value in $_POST['as_admin'] and will set $info_user['as_admin'] = 0 if it is not.

Rather than make repeated calls to $this->db->insert this answer uses $this->db->insert_batch. The array $data is an array of $info_user arrays.

function insert_user()
{
    //get the all of $_POST from $this->input
    //eliminates multiple calls to $this->input
    $posted = $this->input->post();

    //It's possible that no "admin" boxes are checked, if none provide empty array
    $as_admin = isset($posted['as_admin']) ? $posted['as_admin'] : array();

    $data = array();
    if(isset($posted['userid']))
    {
        foreach($posted['userid'] as $key => $value)
        {
            $info_user = array(
                'user_id' => $value,
                'user_name' => $posted['username'][$key],
                'as_admin' => in_array($posted['userid'][$key], $as_admin) ? 1 : 0,
            );
            $data[] = $info_user;
        }
        $this->db->insert_batch('some_table', $data);
    }
}

2 Comments

I've tried this one but it seems not work for the checkbox (or maybe I have the wrong code) but thanks anyway...I have learn much from your code about insert batch...
I tested this and it worked. One very important item to double check is that in each <tr> the value of the checkbox must be the same as the value of the hidden field. In other words, if <input type='hidden' name='userid[]' value='2'> then you must have <input type='checkbox' name='as_admin[]' value=2> in that table row.

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.