CodeIgniter:如何从所需字段中检查其中一个字段是否为空

时间:2013-07-04 05:41:23

标签: php forms codeigniter input

为了继续,必须填写表格字段'room_number','pos_token','banquet_token'中的任何一个。这是要求,我编写了这段代码并最终与代码的其他部分发生冲突,但根本不起作用。

$room_number = $this->input->post('room_number');
$pos_token = $this->input->post('pos_token');
$banquet_token = $this->input->post('banquet_token');

if(isset ($room_number) && strlen($room_number) && ($pos_token) && strlen($pos_token) && ($banquet_token) && strlen($banquet_token) ) {
    return true;
    }
else {
    return true;
    }

4 个答案:

答案 0 :(得分:3)

您需要为输入字段设置规则

$this->form_validation->set_rules('username', 'Username', 'required');
if ($this->form_validation->run() == FALSE)
  {
    //run your code on success here
  }
else
  {
    //run your code on failure here
  }

答案 1 :(得分:2)

您需要在条件失败时返回false

if(isset ($room_number) && strlen($room_number) && ($pos_token) && strlen($pos_token) && ($banquet_token) && strlen($banquet_token) ) {
    return true;
}
else {
    return false;
}

根据您的情况,您需要查看OR条件

if( ( isset ($room_number) && strlen($room_number)) || 
    ( isset ($pos_token) && strlen($pos_token)) || 
    ( isset ($banquet_token) && strlen($banquet_token))
  ) {

答案 2 :(得分:1)

只需在if语句中使用|| OR运算符即可。如果测试失败,则返回false

$room_number = $this->input->post('room_number');
$pos_token = $this->input->post('pos_token');
$banquet_token = $this->input->post('banquet_token');

 if(!empty($room_number) || !empty($pos_token) || !empty($banquet_token))
    return true;
else 
    return false;

答案 3 :(得分:0)

$room_number = $this->input->post('room_number');   
$pos_token = $this->input->post('pos_token');
$banquet_token = $this->input->post('banquet_token');

if($room_number=="" || $pos_token=="" || $banquet_token=="")
    return true;
else 
    return false;