【问题标题】:CodeIgniter How to pass a variable from view to the controllerCodeIgniter 如何将变量从视图传递到控制器
【发布时间】:2015-09-17 08:52:42
【问题描述】:

我目前正在使用 Codeigniter。我正在尝试创建一个注册页面。 注册在视图文件中。填写注册表格后,我想将用户信息发送给控制器。在我希望它检查信息是否有效之后(用户名尚未使用..有效的电子邮件地址等等)。如果是真的,那么将相同的信息发送到添加到数据库中的模型。如果它是假的,那么加载查看注册页面。我发现了很多关于如何将变量从控制器发送到视图(将动态数据添加到 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 之类的信息? 还是我必须找到另一种检查信息的方式? 像控制器 -> 视图 -> 模型?

谢谢

【问题讨论】:

  • 从视图向控制器/方法发出ajax请求
  • 查看完整的工作代码,包括验证、查询、如果表单验证失败则保留数据,在我的个人资料中的链接中,还有一个工作演示 --&gt;
  • @S7_0你有没有检查下面的答案

标签: php html codeigniter


【解决方案1】:

试试这个选项,

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

您的流程:

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

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

通过这种方式,您可以减少数量。控制器和模块之间的调用。

【讨论】:

  • 您好,调用另外两个模块不是更好吗?喜欢:视图:前端控制器:后端/计算模型:SQL查询
【解决方案2】:

你可以这样做

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

【讨论】:

  • 谢谢!否则,在 else 块中。要将以下变量 $name、$last_name 等发送到数据库,我更喜欢在模型中进行。但是我在谷歌上并没有真正找到关于将变量从控制器传递到模型的有趣事情。那么如何将其发送到模型以将它们添加到数据库中? (我知道如何添加它们,但我只是不知道如何从控制器 -> 模型发送变量)谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-08-30
  • 1970-01-01
  • 1970-01-01
  • 2020-02-17
  • 2011-09-14
  • 1970-01-01
相关资源
最近更新 更多