在使用CodeIgniter验证后使用POST数据 [英] Using POST data after validating using CodeIgniter

查看:331
本文介绍了在使用CodeIgniter验证后使用POST数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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?

推荐答案

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

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:

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

这篇关于在使用CodeIgniter验证后使用POST数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆