codeigniter复选框数组处理问题

时间:2011-10-19 21:08:29

标签: arrays forms codeigniter checkbox

我的表单字段是复选框,如下所示:

<input id="abirrules@gmail.com" type="checkbox" checked="checked" value="abirrules@gmail.com" style="float:left;border:2px dotted #00f" name="email[]">
<input id="abirrules1@gmail.com" type="checkbox" checked="checked" value="abirrules1@gmail.com" style="float:left;border:2px dotted #00f" name="email[]">
<input id="abirrules3@gmail.com" type="checkbox" checked="checked" value="abirrules3@gmail.com" style="float:left;border:2px dotted #00f" name="email[]">

但是在控制器中我正在使用 var_dump($ this-&gt; input-&gt; post('email')),它会显示 bool(false)

控制器中的

我有这个方法:

public function referral_email()
{
    $data = $this->input->post('email');
    var_dump($data);exit;
 }

如何在我的控制器中访问此复选框数组?

3 个答案:

答案 0 :(得分:1)

您是否有机会尝试使用表单验证库修剪输出?我有同样的问题,从验证规则中删除'trim'解决了它。

答案 1 :(得分:0)

确保文本框周围的表单标记具有属性method =“post”,否则它可能正在提交到$ _GET数组。我让它使用以下代码:

控制器

public function referral_email()
{
    $data = $this->input->post('email');
    var_dump($data);
}

查看

<form method="post" action="welcome/referral_email">
<input id="abirrules@gmail.com" type="checkbox" checked="checked" value="abirrules@gmail.com" style="float:left;border:2px dotted #00f" name="email[]">
<input id="abirrules1@gmail.com" type="checkbox" checked="checked" value="abirrules1@gmail.com" style="float:left;border:2px dotted #00f" name="email[]">
<input id="abirrules3@gmail.com" type="checkbox" checked="checked" value="abirrules3@gmail.com" style="float:left;border:2px dotted #00f" name="email[]">
<input type="submit" value="Submit" />
</form>

答案 2 :(得分:0)

trim()适用于字符串,但您的$_POST['email']是一个数组。正确的修复应该是在使用表单验证库时将字段名称传递为'email[]'(在您未向我们展示但稍后评论过的代码中)。

http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#arraysasfields

相关问题