不要用name[] 定义set_value(),而是尝试在没有它们的情况下提供它。我创建了一个演示,它对我有用。看看这是否也适合你。
查看
<?php
$attributes = array('method' => 'POST');
echo form_open('home/form', $attributes);
$options = array(
'small' => 'Small Shirt',
'med' => 'Medium Shirt',
'large' => 'Large Shirt',
'xlarge' => 'Extra Large Shirt',
);
$attribute_tag = array(
'class' => 'some-class',
);
// this is the one you should be paying attention to ↓↓
echo form_multiselect('person_tags[]', $options, set_value('person_tags'), $attribute_tag);
$data = array(
'type' => 'text',
'name' => 'email',
'id' => 'email',
);
echo form_input($data, set_value('email'));
echo form_submit('mysubmit', 'Submit Post!');
echo form_close();
?>
控制器
function form(){
$this->load->library('form_validation');
$this->form_validation->set_rules('person_tags[]', 'Tags', 'required');
$this->form_validation->set_rules('email', 'text', 'required');
if ($this->form_validation->run() == FALSE){
$this->load->view('vwHome');
}else{
echo 'No validation error'; die; // save in database(Your logic)
}
}
TL;DR
将您当前的代码更改为-
echo form_multiselect('person_tags[]', $options, set_value('person_tags'), $attribute_tag);