Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 855
S7_0 Avatar asked Feb 01 '26 07:02

S7_0


1 Answers

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.

like image 129
Keval Rathi Avatar answered Feb 02 '26 21:02

Keval Rathi