Codeigniter:从另一个控制器获取变量

时间:2015-02-14 07:29:45

标签: php forms codeigniter

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

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控制器,我在index控制器上创建了dologin功能

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控制器赢了&#39 ; t打破并仍然继续这个过程

我该怎么做? 还是有其他方式来提出我的想法吗?

1 个答案:

答案 0 :(得分:0)

我不确定我是否完全理解你的问题,但这就是我将如何做到这一点:


我们说我们有一个基本的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>

如您所见,提交表单会调用login()属性中定义的控制器myController中的函数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');
        }
    }  
}