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

查看:21
本文介绍了如何从视图模块获取 $_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 AdminModelUser */
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天全站免登陆