【问题标题】:Codeigniter if statement for multiple $this->input->post()多个 $this->input->post() 的 Codeigniter if 语句
【发布时间】:2013-01-05 03:10:17
【问题描述】:

我有代码可以检查用户是否在文本字段中输入了数据:

if ( $this->input->post('current_password') || 
     $this->input->post('new_password') || 
     $this->input->post('repeat_password') ) {
   return true;
} else {
   return false;
}

为什么上面的代码返回false,而下面的代码返回true

if ( $this->input->post('current_password') ) {
   return true;
} else {
   return false;
}

【问题讨论】:

  • 你需要解释更多关于你的代码的逻辑。

标签: php codeigniter if-statement


【解决方案1】:

这应该可行:

if ( ($this->input->post('current_password')) || 
     ($this->input->post('new_password')) || 
     ($this->input->post('repeat_password')) )
{
    return true;
} 
else 
{
    return false;
}

我认为您需要在|| 之间添加一些额外的( )

【讨论】:

  • 不,因为他在最后关闭了,但也许他的逻辑想要那样。
  • 我对此表示怀疑 - 我可以“看到”他正在尝试做什么 - 我认为我的建议应该解决它 - 但我想我们会等待 Amirul 确认跨度>
  • 是的,我想我错过了非常基本的“()”。谢谢大家。
【解决方案2】:

这些类型的字段只能表示一件事。我是否正确您将它们用于用户更改密码表单?在这种情况下:

//Below means that the user required to fill in all 3 of the fields. None of them must return false (be left blank)
if ($this->input->post('current_password') && $this->input->post('new_password') && $this->input->post('repeat_password') ) {
  return true;
} else {
  return false;
}

不过,codeigniter 支持使用表单验证器检查表单字段的更好方法:

$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');

$this->form_validation->set_rules('password', 'Password', 'required');
$this->form_validation->set_rules('newpass', 'New password', 'required');
$this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');

if ($this->form_validation->run() == FALSE) {
  $this->load->view('myform');
} else {
  $this->load->view('formsuccess');
}

【讨论】:

    猜你喜欢
    • 2017-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-07
    • 2017-04-11
    • 1970-01-01
    • 1970-01-01
    • 2012-09-04
    相关资源
    最近更新 更多