0

我有检查用户是否已将数据输入文本字段的代码:

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

2 回答 2

2

这应该有效:

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

我认为您需要( )||

于 2013-01-05T03:28:37.827 回答
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');
}
于 2013-01-05T04:11:06.930 回答