Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using POST data after validating using CodeIgniter

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?

like image 256
James Dawson Avatar asked May 17 '12 19:05

James Dawson


2 Answers

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');
like image 144
birderic Avatar answered Nov 10 '22 01:11

birderic


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

like image 1
matty Avatar answered Nov 10 '22 02:11

matty