在CodeIgniter中正确使用form_validation



我有一个CodeIgniter注册系统,但目前我无法控制电子邮件和密码。用户无需输入电子邮件或密码即可注册。我在我的注册控制器中有一个index()和一个register_user函数(),但重定向没有成功现在我有以下代码:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Signup extends CI_Controller {
    public function index()
    {
        if(!isset($this->session->userdata['sessiondata']['user_id']))
        {
             $this->load->helper(array('form', 'url'));
             $this->load->library('form_validation');
             $this->form_validation->set_rules('email', 'Email', 'required');
             $this->form_validation->set_rules('password', 'Password', 'required',
                    array('required' => 'You must provide a %s.')
             );
               if ($this->form_validation->run() == FALSE)
               {
                    $this->load->view('signup-view');
               }
               else
               {
                    $this->load->view('home-view');
               }
        }else{
            if (intval($this->session->userdata['sessiondata']['user_type']) == 1) {
                redirect(base_url().'admin');
            } else {
                redirect(base_url().'home');
            }
        }
    }
    function register_user(){
        $this->load->library('custom_u_id');
        $data = array('user_id' => $this->custom_u_id->construct_id('USR'),
                      'name' => $_POST['name'],
                      'email' => $_POST['email'],
                      'password' => $_POST['password'],
                     );
        $this->load->model('signup_model');
        $user_details = $this->signup_model->register_user($data);
        if (!empty($user_details)){
            $user_data =  array
            (
                'user_id' => $user_details['user_id'],
                'email' => $user_details['email'],
                'name' => $user_details['name'],
                'user_type' => $user_details['user_type'],
            );
            $this->session->set_userdata('sessiondata',$user_data);
            if (intval($user_details['user_type']) == 1) {
                redirect(base_url().'admin');
            } else {
                redirect(base_url().'home');
            }
        } else{
            redirect('login');
        }
    }// end of function login
}

我需要把form_validation在我的register_user函数?我试过了,但是支票不工作了…

我还认为<?php validation_errors();?>函数和<?php form_open(base_url().'signup');?>

看你的代码,我认为你想把register_user()内验证TRUE,因为查询是在该方法。

所以试着把你的代码改成这样:
public function index()
    {
        if(!isset($this->session->userdata['sessiondata']['user_id']))
        {
             $this->load->helper(array('form', 'url'));
             $this->load->library('form_validation');
             $this->form_validation->set_rules('email', 'Email', 'required');
             $this->form_validation->set_rules('password', 'Password', 'required',
                    array('required' => 'You must provide a %s.')
             );
               if ($this->form_validation->run() == FALSE)
               {
                    $this->load->view('signup-view');
               }
               else
               {
                    $this->register_user();
               }
        }else{
            if (intval($this->session->userdata['sessiondata']['user_type']) == 1) {
                redirect(base_url().'admin');
            } else {
                redirect(base_url().'home');
            }
        }
    }

,确保你的表单动作像这样:

<form action="<?=site_url('/signup/index');?>">

相关内容

  • 没有找到相关文章

最新更新