【问题标题】:Codeigniter : get variable from another controllerCodeigniter:从另一个控制器获取变量
【发布时间】:2015-02-14 07:29:45
【问题描述】:

我有以下控制器来显示登录表单(第一页)

public function index()
{
    $data['form_type']='dologin';
    $data['username_label']='Username';
    $data['password_label']='Password';
    $data['username_name']='usernamenya';//name of the textbox username
    $data['password_name']='passwordnya';//name of the textbox password
    $data['username_value']='';
    $data['password_value']='';
    $data['fieldset_text']='Silahkan Login';        
    $data['fieldset_close_text'] = '</div></div>';
    $data['form_close_text']='</div></div>';
    $data['clear_name']='Hapus';
    $data['submit_name']='Login';

    $this->load->helper('form');
    $this->load->view('header');
    $this->load->view('content_login',$data);
    $this->load->view('footer');        
}

点击按钮后会重定向到dologin控制器,我在dologin控制器上创建了index函数

class dologin extends CI_Controller {

public function index()
{
    $this->load->library('form_validation');
    $this->form_validation->set_rules('','','');

    if ($this->form_validation->run() == FALSE)
    {
        $this->load->view('myform');
    }
    else
    {
        $this->load->view('formsuccess');
    }
}   
      }

在需要控制名称的form_validation_rules 中,我想从另一个控制器中获取它,因此,如果我们更改index 控制器中的名称,dologin 控制器不会中断并继续该过程

我该怎么做? 或者有没有其他方法可以让我的想法?

【问题讨论】:

  • 我不明白。如果字段是动态发送的,您如何知道需要应用什么验证规则?
  • @AdrienXL 这就是为什么,我想从index 控制器调用usernamenyapasswordnya,所以,如果其他时间我们将usernamenya 更改为stack_usernamenya,那会赢没问题。 . .
  • 您可以在包含serialize($data) 的表单中添加隐藏输入,当您将表单发布到dologin 控制器时,您可以取消序列化它。但更好的方法是创建一个包含您的表单的config/form.php

标签: php forms codeigniter


【解决方案1】:

我不确定我是否完全理解了你的问题,但我会这样做:


假设我们有一个用于登录的基本 html 表单,我们在其中要求输入电子邮件和密码:

<form method="post" action="<?php echo site_url("myController/login"); ?>">
    <input type="text" name="login_mail" required>
    <input type="password" name="login_pass" required>
    <button type="submit">Login</button>                
</form>

如您所见,提交表单将调用myController 控制器中的函数login(),如action 属性中定义的那样。

让我们看看login()函数:

class MyController extends CI_Controller
{
    public function login()
    {
        $this->load->library('form_validation');

        //Here we set the validation rules for our two fields. We must use the same names as defined in out html for the `name` attribute.
        $this->form_validation->set_rules('login_mail', 'Email', 'trim|required|valid_email|xss_clean');
        $this->form_validation->set_rules('login_pass', 'Password', 'trim|required|xss_clean');

        if ($this->form_validation->run()) //Success
        {
            //Get the submited values
            $email = $this->input->post("login_mail");
            $password = $this->input->post("login_pass");

            //Your stuff here

            $this->load->view('formsuccess');            
        }
        else
        {
            $this->load->view('myform');
        }
    }  
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-21
    • 2020-11-23
    • 2021-11-02
    相关资源
    最近更新 更多