如何从视图模块获取$ _POST请求? [英] How to get a $_POST request from view module?

查看:58
本文介绍了如何从视图模块获取$ _POST请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从视图模块的表中获取复选框,但无法使其正常工作.这是表格:

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>

整个.phtml: http://pastebin.com/8paJZgjb

我想获取users[]数组并在控制器内使用它的元素,但是我找不到找到它的方法.到目前为止,这是我尝试过的:

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;
        }

整个功能: http://pastebin.com/pE4afemx

...但是显然它是空的. var_dump显示邮件表单中的元素,但没有用户的迹象:

...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)

有什么建议吗?

推荐答案

从pastebin链接中的代码中,您在页面上有两个表单.一个在第16行开始,包含您的用户复选框,然后在第61行结束.然后第二个在第77行开始,在第79行结束.我猜这是您在第86行上的javascript提交按钮提交的一个

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.

由于您要提交第二个表单,因此只会发布其数据.如果要提交所有内容,则所有内容都必须采用一种形式.理想情况下,您要将用户复选框从该模板中移出,并将它们作为$form的一部分输出,但是以这种方式将其保留在表格布局中可能会比较棘手,因此,更容易解决的方法是将包含以下内容的表格移到该表格中用户复选框,以便在echo $this->formCollection($form);之后但在该表单以echo $this->form()->closeTag();结尾之前将其输出.删除第16和61行上的表单标签,因为您只需要一种表单.

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).")");
    }

这篇关于如何从视图模块获取$ _POST请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆