使用CodeIgniter验证后使用POST数据

时间:2022-10-06 16:14:10

I have a signup form where I'm validating user input. Here's my controller:

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

<?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 */

The problem is with this line: $this->Users_model->add_user();. I want to pass the username, email and password to my Users model to add the user to my database, but I'm not sure how I can get the POST data into that method. Normally I'd use $_POST['username'] etc but CodeIgniter has run some functions on the input values (trim(), xss_clean etc). How can I get these values and pass them into my add_user() method?

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

3 个解决方案

#1


14  

The CodeIgniter input class allows you to get the POST data after it has been filtered by the form validation library. In your controller you would do the following:

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

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

#2


1  

even easier, create an array and send the array to the model

甚至更容易,创建一个数组并将数组发送到模型

#3


0  

You can use the input class as well as the helper function set_value('email')

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

#1


14  

The CodeIgniter input class allows you to get the POST data after it has been filtered by the form validation library. In your controller you would do the following:

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

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

#2


1  

even easier, create an array and send the array to the model

甚至更容易,创建一个数组并将数组发送到模型

#3


0  

You can use the input class as well as the helper function set_value('email')

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