我在一個頁面中有兩個表單,它們是註冊表單和登錄表單,我爲每個表單定義了一個名稱,所以我可以單獨發佈它,但是我怎樣才能只寫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