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.
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 theforeacharen’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:
Then
$deleteWhatin each iteration contains the string that was posted, whatever was in thevalueof that checkbox.This is the array:
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 returnsFALSE)<input name="somename[hello]"><input name="somename[hello][]">will post an array of arrays, etc.