This question already has an answer here:
I want to get the checkboxes from a table in a view module, but I can't get it to work. Here's the table:
<form action="" method="post">
<TABLE id="dt_basic" class="table table-striped table-bordered table-hover dataTable">
<THEAD>
<TR>
<TH></TH>
<TH>ID</TH>
<TH>Client</TH>
<TH>User</TH>
<TH>Role</TH>
<TH>Projects</TH>
</TR>
</THEAD>
<TBODY>
<?php
/* @var $item Admin\Model\User */
foreach ($this->users as $item) {
?>
<TR>
<TD class='admin'><input type="checkbox" name="users[]" value="<?php echo $item->id; ?>" class="users" checked/></TD>
<TD class='admin c'><?= $item->id ?></TD>
<TD class='admin'><?= $item->getClientObject() ?></TD>
<TD class='admin'>
<img src="<?= $item->getAvatarPath() ?>" alt="me" class="online avatar"/>
<a title='details'
href='/admin/user/detail/id/<?= $item->id ?>'><?= $item->name . " " . $item->surname ?> </a>
</TD>
<TD class='admin'><?= $item->getRoleObject() ?></TD>
<TD class='admin'>
<a title='projects' class='btn btn-default btn-sm'
href='/admin/user/project/id/<?= $item->id ?>'><i
class='fa fa-icon-fix fa-briefcase'></i></a>
<?
foreach ($this->projects[$item->id] as $i => $project) {
echo $project->getName();
if ($i != sizeof($this->projects[$item->id]) - 1)
echo " • ";
}
?>
</TD>
</TR>
<?php
}
?>
</TBODY>
</TABLE>
</form>
I want to get the users[]
array and use its elements inside the controller, but I can't find a way to get it, any suggestions?
</div>
You should to replace the row as follows:
<TD class='admin'><input type="checkbox" name="users[<?php echo $item->id; ?>]" class="users" checked/></TD>
In this way you'll receive the users where the keys are id-s, but values will be 'on'. You'll receive only checked users.
Regarding the question below:
if (!empty($_POST["users"])) {
foreach (($_POST["users"]) as $selectedUsers=>$blabla) {
$users = $this->getUserTable()->fetchAll(false, "id=" . $selectedUsers);
}
}