我在一个页面中有两个表单,它们是注册表单和登录表单,我为每个表单定义了一个名称,所以我可以单独发布它,但是我怎样才能只写1 form_validation-run() ?Codeigniter中的多个表单和表单验证
public function index()
{
//load form_validation and session library
$this->load->library(array('form_validation','session'));
if ($this->input->post('register')) {
$this->form_validation->set_rules('first_name', 'First name', 'required');
$this->form_validation->set_rules('last_name', 'Last name', 'required');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
$this->form_validation->set_rules('password', 'Password', 'required|min_length[6]');
if ($this->form_validation->run() !== FALSE){
// to create an account
} else {
$this->session->set_flashdata('msg', validation_errors('<div>','</div>'));
redirect('/','location');
}
} elseif ($this->input->post('login')) {
$this->form_validation->set_rules('email', 'Email', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
if ($this->form_validation->run() !== FALSE) {
// to get login
} else {
$this->session->set_flashdata('msg', validation_errors('<div>','</div>'));
redirect('/','location');
}
}
$this->load->view('templates/header');
$this->load->view('pages/index');
$this->load->view('templates/footer');
}
学会接受答案 –
OK,.................... – vzhen