How to get a $_POST request from view module?

2019-02-26 21:15发布

问题:

I want to get the checkboxes from a table in a view module, but I can't get it to work. This is 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[<?php echo $item->id; ?>]" value=<?php echo $item->id; ?> 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>

The whole .phtml : http://pastebin.com/8paJZgjb

I want to get the users[] array and use its elements inside the controller, but I can't find a way to get it. Here's what I've tried so far:

           if (!empty($_POST["users"])) {
            foreach (($_POST["users"]) as $selectedUsers) {
                $users = $this->getUserTable()->fetchAll(false, "id=" .  $selectedUsers);
            }
        } else {
            $this->cache->error = "Please choose at least one user.";
            return $this->view;
        }

The whole function: http://pastebin.com/pE4afemx

...but apparently it's empty. var_dump shows the elements from the mailing form, but no sign of the users:

array (size=2)
'subject' => string 'hello' (length=5)
'message' => string 'message' (length=7)

Any suggestions ?

回答1:

From the code in your pastebin link, you have two forms on the page. One starts on line 16, contains your user checkboxes, and then ends on line 61. Then the second starts on line 77 and ends on line 79. I'm guessing this the one being submitted by the javascript submit button you have on line 86.

Since you're submitting the second form, only its data gets posted. If you want everything to be submitted, it all needs to be in one form. Ideally you'd move the user checkboxes out of this template they're output as part of $form, but it might be tricky to keep them in a table layout that way, so the easier fix would be to move the table that contains the user checkboxes so it is output after the echo $this->formCollection($form);, but before that form ends with echo $this->form()->closeTag();. Get rid of the form tags you have on line 16 and 61, since you only want one form.

Edit: To fix the other issue, try changing your controller code to this:

if (!empty($_POST["users"])) {
    $userIDs = array();
    foreach ($_POST["users"] as $selectedUser) {
        if (is_numeric($selectedUser) && $selectedUser > 0) {
            $userIDs[] = $selectedUser;
        }
    }

    if (count($userIDs) > 0) {
        $users = $this->getUserTable()->fetchAll(false, "id IN (".implode(', ', $userIDs).")");
    }