Search code examples
phpsqlcodeigniter

How to access data from form validating controller in codeigniter?


I followed the tutorial for making a registration form with validation, with 2 views and 1 controller.

I wrote this in the controller:

function index()
{
    $this->load->helper(array('form', 'url'));
    $this->load->library('form_validation');

    $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[3]|max_length[16]|xss_clean|callback_username_check');
    $this->form_validation->set_rules('password', 'Password', 'trim|required|matches[passconf]|md5');
    $this->form_validation->set_rules('passconf', 'Password Confirmation', 'trim|required');
    $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');

    if ($this->form_validation->run() == FALSE)
    {
        $this->load->view('register_form');
    }
    else
    {
        $this->load->view('register_success');

        $data = array(
        'IDUser' => NULL ,
        'Username' => "$username" ,         
        'Password' => 'password' ,
        'Email' => 'email' ,
        'Gender' => 'gender' ,
        'Birthday' => 'bday' ,
        );

        $this->db->insert('Accounts', $data);

    }


}

First it is executes a working sql insert. After, a validation. Nevertheless, the values loaded from the database are not those from the validation. Instead, they are the same plain text in the array.

I don't want to get the values directly from the form/view with POST; that's pointless. What do i do? I'm new to Codeigniter and not that familiar with OOP PHP.


Solution

  • You just have to use Codeigniter Input class : https://codeigniter.com/userguide3/libraries/input.html

    So in your case:

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