0

I have checkboxes in my form, which returns me string instead of array, as it should. Can anyone tell me what is wrong with my code?

Here is the code from the view:

    <?php echo form_open('backOfficeUsers/deleteMoreUsers');?>
            <table border="0" cellpadding="4" cellspacing="1" bgcolor="#02659E" width="500">
                <tr bgcolor="#E9E8ED">
                    <td align="center">
                        <b>User ID</b>
                    </td>
                    <td align="center">
                        <b>User Name</b>
                    </td>
                    <td align="center">
                        <b>Password</b>
                    </td>
                    <td align="center">
                        <b>Select for delete</b>
                    </td>
                    <td align="center">
                        <b>Delete</b>
                    </td>
                </tr>

<?php

    foreach ($users as $key => $user) 
    {
        echo form_open('backOfficeUsers/deleteUser');
        echo form_hidden('dpage', 'backOfficeUsers/displayAllUsers');
        echo form_hidden('rid', $user['id']);
        echo"<tr bgcolor='#E9E8ED'>";
        echo "<td>" . anchor("backOfficeUsers/displayEditUserForm/$user[id]/", $user['id']) . "</td>";
        echo "<td>" . $user['username'] . "</td>  ";
        echo "<td>" . $user['password'] . "</td>  ";
         echo "<td>" . form_checkbox('userdelete[]', $user['id']) . "</td>  ";
        $confirm = "onclick='return confirmSubmit();'";
        echo"<td>";
        echo form_submit('submit', 'Delete', $confirm);
        echo"</td></tr>";
        echo form_close();
    }
?>
            </table>
            <?php echo form_submit('submit', 'Delete All Selected Users');?>
            <?php echo form_close();?>
        </div> 

And when I make var dump from my controller, I am getting string string(3) “200” (while 200 is the row id.

Here is the code of the controller:

foreach ($this->input->post('userdelete') as $row){
   $deleteWhat = $row;  
   var_dump($deleteWhat);
   die(); 
}

This print string(3) and the id of the first row.

4
  • You didn't show what you assigned to $deleteWhat... How would we know what it should contain? Commented Mar 12, 2012 at 7:46
  • Sorry for the mistake... Commented Mar 12, 2012 at 7:59
  • foreach ($this->input->post('userdelete') as $row){ $deleteWhat = $row; } Commented Mar 12, 2012 at 8:01
  • I just noticed you have form_open() inside another form_open() which is going to screw things up for you, did you realize that? Commented Mar 12, 2012 at 8:16

1 Answer 1

1

EDIT: I just caught this:

You have nested <form> tags which is invalid and prone to unexpected behavior, you must fix that first (high priority). I can't tell which one you need to remove because I don't know your application, but it looks like the ones inside the foreach aren't supposed to be there, otherwise you wouldn't need to post an array of values.

Anyways, assuming that wasn't the case...


If this is your code:

foreach ($this->input->post('userdelete') as $row){
    $deleteWhat = $row;
}

Then $deleteWhat in each iteration contains the string that was posted, whatever was in the value of that checkbox.

This is the array:

$this->input->post('userdelete')

It contains everything that was posted from form_checkbox('userdelete[]', $user['id'])

Your code is working fine.

Just remember a few things:

  • <input name="somename[]"> will post as an array of values because of the brackets.
  • $_POST['somename'] is equal to $this->input->post('somename'), except when the value isn't set (the first generates an undefined variable notice, the second returns FALSE)
  • You may specify keys like: <input name="somename[hello]">
  • <input name="somename[hello][]"> will post an array of arrays, etc.
  • Checkboxes don't post anything at all unless they are checked.
Sign up to request clarification or add additional context in comments.

9 Comments

'code'$deleteWhat = $this->input->post('userdelete'); var_dump($deleteWhat); die(); 'code' Produce this:array(1) { [0]=> string(3) "200" }
That looks right to me, not sure what you see wrong with it, it's an array with one item: a string "200". I suggest you edit your post rather than putting your code in comments where it can't be understood. Did you notice the nested <form> tags?
I know that I have form within a form...first one is to delete all selected users, and the second is to delete only one user. And I need both, so the user can delete one or more records. The problem in my code is that even if I check 3 or more checkbox, I always get only the first checked checkbox, not the rest of the checked checkboxes.
...but you can't write it that way. Forms cannot be within other forms in HTML, and you will get strange results if you do so. You're only getting one item because as soon as the parser sees </form>, the outer one is closed. I sympathize with your cause, but you need to find another way to do it. Sorry, not my rules.
So, which options I do have now? Use a jquery to get all checked checkboxes?
|

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.