2015-09-17 212 views
3

我目前正在使用Codeigniter。我正在尝试创建一个注册页面。 注册在视图文件中。在填写注册表格后,我想将用户的信息发送给控制器。并且在我希望它检查信息是否有效之后(用户名尚未使用..有效的电子邮件地址等)。如果它是真的,则将相同的信息发送到添加到数据库的Model。如果它是假的,所以加载视图注册页面。我发现很多话题都是关于如何从控制器发送变量来查看(在CodeIgniter网站上向View添加动态数据)或控制器来进行建模,但没有发现任何有关view-> controller的有用信息。这里是我想做的事:CodeIgniter如何将视图中的变量传递给控制器​​

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

这是我的形式:

<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> 

这是我的控制器文件

<?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 :) 
    } 
} 

我不知道这是否是更好的检查用户在模型中的信息。因为我知道模型处理数据库和查询。 否则控制器会处理计算部分,因此控制器可能更适合执行此任务。 但没关系。是否有可能发送像view ----> controller这样的信息? 还是让我找到另一种检查信息的方式? 像控制器 - >查看 - >模型?

感谢

+1

做Ajax请求控制器/法从视图 –

+1

看到完整的工作代码验证,查询,保存数据,如果表单验证失败,在我的个人资料链接,也有一个工作演示' - >' – Viral

+0

@ S7_0你检查下面的答案 –

回答

1

尝试使用此选项,

首先从视图中的数据发送到控制器和控制器检查数据是否有效(服务器端验证),如果有效,则发送到模块说函数名check_user_data如果该函数检查密码是否匹配,则用户名不是用户,如果发现它是ok然后调用新函数说save_user_data并插入用户数据,如果它是不好然后从该函数返回false到控制器。

你的流程:

在您选择的用户数据发送到控制器,然后调用模块函数来检查用户信息是否正确,如果是,则发送的数据插入到模块。

这里您正在对模块进行两次调用。

用这种方法你可以减少no。控制器和模块之间的通话。

+0

你好, 是不是更好地调用两个其他模块留组织?像: 查看:前端 控制器:后端/计算器 型号:SQL查询 –

1

你能做到这样

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

     } 
} 

在模型中,应该有插入功能

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

谢谢!否则,在else块中。为了将以下变量$ name,$ last_name等发送到数据库,我宁愿在模型中执行它。 但我并没有真正发现谷歌关于从控制器到模型的传递变量有趣的事情。 那么如何将它发送到模型以将它们添加到数据库? (我知道如何添加它们,但我不知道如何发送变量从控制器 - >型号) 谢谢 –

相关问题