S7_0
S7_0

Reputation: 1183

CodeIgniter How to pass a variable from view to the controller

I'm currently working with Codeigniter. I'm trying to create a registration page. The registration is in the view file. After fill the form for the registration, I would like to send the user's information to the controller. And after I want it to check if the information are valid (username are not already use.. valid email adresse and so on). If it's true so send the same information to the Model which add to the database. If it's false so load view registration page. I found a lot of topic talking about how to send variable from controller to view (Adding Dynamic Data to the View on CodeIgniter website) or controller to model but didn't find anything helpful about view->controller. Here's is what I want to do:

    VIEW                         CONTROLER
    _______________              ______________________
    |REGISTRATION:| -----------> |Check valid argument|
    ---------------              ----------------------
             /|\                  |        |
              |___________________| If it's valid information
   (if no valid)                           |
                                           |
                                          \ /
                                         MODEL
                                  ____________________________
                                  |Add information to database|
                                  -----------------------------

This is my form:

<h1>create a new account</h1>
<?php echo validation_errors();?>
<?php echo form_open('index.php/add_db');?>
    <label for='name'>Name</label>
    <input type='text' size='20' id='name' name='name'/>
    <br />
    <label for='last_name'>Last_Name</label>
    <input type='text' size='20' id='last_name' name='last_name'/>
    <br />
    <label for='username'>Username</label>
    <input type='text' size='20' id='username' name='username'/>
    <br />
    <label for='password'>Password</label>
    <input type='password' size='20' id='username' name='password'/>
    <br />
    <label for='confirmation_password'>Password confirmation</label>
    <input type='password' size='20' id='password' name='password'/>
    <br />
    <input type="submit"/>
</form>

And here's my controller file

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Add_db extends CI_Controller 
{
    function __construct()
    {
        parent:: __construct();
        $this->load->database();
    }

    function index()
    {
        //check if the password does match
        //check before if the username is not already user
        //open the model function which add to the database :)
    }
}

I don't know if it's better to check the user's information in the model. Because I know that the model handle the database and the query. Otherwise the controller handle calcul part so maybe the controller is more suitable for this task. But doesn't matter. Is it possible to send information like view---->controller ? Or have I to find another way for check information ? Like controller -> view -> model ?

Thanks

Upvotes: 3

Views: 2657

Answers (2)

Janaka
Janaka

Reputation: 408

You can do it in this way

function __construct()
    {
        parent:: __construct();
        $this->load->database();
        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');
    }

  function index(){

        //first set the validation rules
        $this->form_validation->set_rules('name', 'Name', 'required');
        $this->form_validation->set_rules('last_name', 'Last Name', 'required');
        $this->form_validation->set_rules('username', 'User Name', 'required');
        //re_password is the input field name of confirm password
        $this->form_validation->set_rules('password', 'Confirm password', 'trim|required|matches[re_password]');

            if ($this->form_validation->run() == FALSE) {
                //return to data form again
                $this->load->view('myform');
            }
            else{
                //if validation is ok, then save data in db
                //this is the way that catch, data coming from view in the controller
                $name      = $this->input->post('name');
                $last_name = $this->input->post('last_name');
                $username  = $this->input->post('username');
                $password  = $this->input->post('password');

                // then you can send these date to database
                //to send to db using array, array keys should match with the db table column names
              $data_to_db['name']      = $name;
              $data_to_db['last_name'] = $last_name;
              $data_to_db['username']  = $username;
              $data_to_db['password']  = $password;

              //this model should be loaded in this controller
              //send data from controller to model
               $this->model_name->insert($data_to_db);  

         }
}

In your model, there should be the insert function

 public function insert($data_to_db){
        $this->db->insert('table_name', $data_to_db);
    }

Upvotes: 1

Keval Rathi
Keval Rathi

Reputation: 986

Try this option,

First from view send data to controller and in controller check data is valid or not (server side validation) if valid then send to module say function name check_user_data if that function check if the password does match, username is not already user if you find it's ok then call new function say save_user_data and insert user data, if it's not ok then return from that function false to controller.

Your flow:

In your option send user data to controller and then call module function to check user information is correct or not if yes then send data to module for insertion.

Here you are making two calls to module.

In this way you can reduce no. of call between controller and module.

Upvotes: 1

Related Questions