5

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

<?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()方法中?

4

3 回答 3

14

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

$username = $this->input->post('username');
$email = $this->input->post('email');
$password = $this->input->post('password');
于 2012-05-17T19:05:04.300 回答
1

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

于 2013-08-13T14:47:47.523 回答
0

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

于 2012-05-18T05:23:46.593 回答