【问题标题】:codeigniter checkbox array handling issuecodeigniter 复选框数组处理问题
【发布时间】:2011-10-19 21:08:28
【问题描述】:

我有如下复选框的表单字段:

<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->input->post('email')) 时,它会显示 bool(false)

在控制器中我有这个方法:

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

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

【问题讨论】:

  • 你能发布你的控制器代码吗?
  • 我刚刚更新了@Colin
  • 如何/在哪里加载视图?
  • 我正在从另一个控制器加载我的视图。并且表单数据正在提交给这个控制器。
  • 不确定这是否有帮助,但您的 id="" 属性中可能不应包含“@”字符。

标签: arrays forms codeigniter checkbox


【解决方案1】:

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

【讨论】:

  • Bingo @Talha...这正是我正在做的...感谢解决方案的伙伴。
【解决方案2】:

确保文本框周围的表单标签具有属性 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>

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 2015-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多