在使用 CodeIgniter 进行验证后使用 POST 数据


Using POST data after validating using CodeIgniter

我有一个注册表单,我正在验证用户输入。这是我的控制器:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Register extends CI_Controller {
    public function index()
    {
        $this->load->model('Users_model');
        $this->load->helper('form');
        $this->load->library('form_validation');
        $data['page_title'] = 'Register';
        $this->load->view('header', $data);
        // Set form validation rules
        $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[5]|max_length[16]|xss_clean|callback_username_check');
        $this->form_validation->set_rules('email', 'Email', 'trim|required|min_length[5]|max_length[64]|valid_email|callback_email_check');
        $this->form_validation->set_rules('password', 'Password', 'required');
        $this->form_validation->set_error_delimiters('<span class="error">', '</span>');
        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('register', $data);
        }
        else
        {
            // Add the user to the database
            $this->Users_model->add_user();
            $this->load->view('register_success', $data);
        }
        $this->load->view('footer', $data);
    }
    /* Functions to check username and email */
}
/* End of file register.php */
/* Location: ./application/controllers/register.php */

问题出在这条线上:$this->Users_model->add_user(); .我想将用户名、电子邮件和密码传递给我的用户模型,以将用户添加到我的数据库中,但我不确定如何将 POST 数据放入该方法中。通常我会使用$_POST['username']等,但CodeIgniter对输入值(trim()xss_clean等)运行了一些函数。如何获取这些值并将其传递到我的add_user()方法中?

CodeIgniter 输入类允许您在表单验证库过滤 POST 数据后获取该数据。 在控制器中,您将执行以下操作:

$username = $this->input->post('username');
$email = $this->input->post('email');
$password = $this->input->post('password');

更简单的是,创建一个数组并将数组发送到模型

您可以使用输入类以及帮助程序函数set_value('email')