多重选择验证无效



这里我已经对多选进行了验证,即使我从select字段中选择值并提交,也要选择字段并使用codeigniter 3.0.6。搜索了很多,但找不到解决方案;这是我的控制器

function online_booking() {
  $this->form_validation->set_rules('course', 'Course', 'required');
  $this->form_validation->set_rules('branch', 'Branch', 'required');
  if($crs = $this->input->post('course'))
  {
    $course = implode(',',$crs);
    var_dump($course); 
  }      
  if ($this->form_validation->run() == TRUE) 
  {
    $this->load->model('branch_model');
    $data = array('course' => $course, 'branch' => $this->input->post('branch'));
    $id = $this->branch_model->insert_enquiry($data);
    if ($id) 
        {
      $this->session->set_flashdata('message', 'Booking has been Succeed,We will contact you shortly.');
      redirect('online-booking');
        } 
        else 
        {
      $this->session->set_flashdata('message', 'Error has been occured,try again.');
      redirect('online-booking');
        }
  }
$data['active'] = 'online';
$data['program'] = $this->home_model->get_program();
$this->load->view('online_bookin', $data);
}

这是我的浏览页面

<div class="form-group col-md-6">
            <label for="exampleInputEmail1">Course to be opted*</label>
            <select class="form-control" name="course[]" id="course" multiple="multiple">
              <option value="">Select Course</option>
              <?php foreach ($program->result() as $row) if($row->parent_id !=0) {?>
              <option value="<?php echo $row->id; ?>" <?php echo set_select('course',$row->id) ?> ><?php echo $row->name; ?></option>
              <?php  } ?>
            </select>
          </div>

这是我的回调函数

function is_multiple_select() {
$crs=$this->input->post('course');
if(!$crs)
 {
   $this->form_validation->set_message('is_multiple_select','You did not select any course to upload.');
  return false;
 }
 else
 {
  return true;
 }

}

这是我的型号

public function insert_enquiry($data=array())
{
   if($this->db->insert(`enquiryform`,$data))
     {
       return $this->db->insert_id();
     }
     else
     {
        return false;
     }

}

为什么要使用以下代码:

if($crs = $this->input->post('course'))
  {
    $course = implode(',',$crs);
    var_dump($course); 
  }

拆下它,检查它是否工作。或者将其放置在$this->form_validation->run() == TRUE块中。

function is_multiple_select() {
   $crs=$this->input->post('course');
   if(!empty($crs) && count($crs)>0)
   {      
    return true;
   }
   else
   {
     $this->form_validation->set_message('is_multiple_select','You did not select any course to upload.');
     return false;
   }    
}

//更改

if ($this->form_validation->run() == TRUE) 
 {
//You can't save an array directly.
$data = array('course' => implode(",",$this->input->post('course') ), 'branch' => $this->input->post('branch') );
$this->db->insert(`enquiryform`,$data);
$insert_id = $this->db->insert_id();
}

在检索时爆炸课程。

相关内容

  • 没有找到相关文章

最新更新