3

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

3
  • 1
    do ajax request to the controller/method from the view Commented Sep 17, 2015 at 9:11
  • 1
    see full working code with validation, queries, preserve data if form validation fails, in links in my profile, also have a working demo --> Commented Sep 17, 2015 at 13:50
  • @S7_0 have you check below answer Commented Sep 18, 2015 at 9:02

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Hello, Is is not better to call two other module for stay organise ? Like: View: for the front end Controller: backend/calcul Model: SQL query
1

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);
    }

1 Comment

Thank you ! Otherwise, in the else block. For send the following variable $name, $last_name etc etc to the database, I prefer to do it in the model. But I don't really find interesting thing on google about pass variable from controller to model. So How can I send it to the model for add them to the database ? (I know how to add them, but I juste don't know how to send variable from controller -> model) Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.