Codeigniter if语句多个$ this-> input-> post()

时间:2013-01-05 03:10:18

标签: php codeigniter if-statement

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

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

2 个答案:

答案 0 :(得分:2)

这应该有效:

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

我认为您需要在( )

之间添加一些空格||

答案 1 :(得分:1)

这些类型的字段只能意味着一件事。我是否正确您使用它们作为用户更改密码表单?在那种情况下:

//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');
}