【问题标题】:Codeigniter form_open()Codeigniter form_open()
【发布时间】:2014-03-10 17:37:45
【问题描述】:

我有 form_open 视图,我想将此数据发送到控制器,但我不知道如何在控制器中读取和显示此数据。

<?php echo form_open('Login/login'); ?>
 <h4 class="form-signin-heading"> <i class="fa fa-user"> &nbsp Panel logowania: </i></h4>
 <input type="email" name="username" class="form-control" style="margin-bottom:7px;" placeholder="Email address" required autofocus>
 <input type="password" name="password" class="form-control" placeholder="Password" required>
  <label class="checkbox">
  <small>
  <input type="checkbox" value="remember-me"> Zapamiętaj mnie
  </small>
  </label>
  <button class="btn btn-sm btn-primary btn-block" style="margin-bottom: 5px;" type="submit">Logowanie</button>
<?php  echo form_close(); ?>

如何在控制器中显示输入值?

【问题讨论】:

标签: forms codeigniter


【解决方案1】:

在控制器中,您不必显示数据,您只需从表单中获取数据并对其进行操作以保存到数据库中或显示在另一个视图中。

要从表单中获取数据,您只需使用输入类:

$this->input->post();

$this->input->get();

基于您在表单中设置的方法。

最好的方法是使用带有表单验证类的表单助手,这样你可以在控制器之前检查输入。

让我们使用您的表单,并假设这是生成它的控制器:

public function login()
{
    //Load library
    $this->load->library('form_validation');

    //Set the rules
    $this->form_validation->set_rules('username', 'Username', 'required');
    $this->form_validation->set_rules('password', 'Password', 'required');

    if( $this->form_validation->run() )
    {
        //The form is correct
        $email = $this->input->post('username'); //I get this from you form
        $password = $this->input->post('password');

        //Do what you need to do
    }
    else
    {
        //Show the form again and in the view handle the error messages
        $this->load->view('login_view')
    }
}

【讨论】:

    【解决方案2】:

    要访问控制器中的 POST 数据,您可以使用系统自动加载的input class。您想要执行以下操作

    class Login extends CI_Controller
    {
        function login()
        {
            $username = $this->input->post('username');
            $password = $this->input->post('password');
    
            //Do something with username and password
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-09
      • 2012-06-14
      • 2011-07-14
      • 2016-04-20
      • 1970-01-01
      • 1970-01-01
      • 2020-08-15
      • 2012-12-16
      相关资源
      最近更新 更多