簡體   English   中英

有沒有一種方法可以在控制器中管理整個表單,然后僅在視圖中打印出一個“表單”變量?

[英]Is there a way to manage whole the form in the controller and then just print out a “form”variable in the view?

在codeigniter中創建表單時,我不想在視圖中使用大量php-stuff。

控制器

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends CI_Controller {

    public function index()
    {
                //Create login and register-forms
                $this->load->helper('form');

                $loginform_attributes = array('id' => 'login-form');                
                $data['loginform_attributes'] = $loginform_attributes;

                $this->load->view('home', $data);
    }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */

風景...

echo form_open('login/register', $loginform_attributes);

有沒有一種方法可以在控制器中管理整個表單,然后僅在視圖中打印$form 當您嘗試類似$x = form_open('login/register', $loginform_attributes)時,似乎什么也不會返回; 在控制器中。

我建議使用部分視圖,並將內容作為變量傳遞給Controller中的主視圖,如下所示:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends CI_Controller
{
    public function index()
    {
        //Create login and register-forms
        $this->load->helper('form');

        $partial['loginform_attributes'] = array('id' => 'login-form');

        //Load the login form and assign the output to a variable
        $data['form'] = $this->load->view('partial/forms/login', $partial, TRUE);

        $this->load->view('home', $data);
    }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */

配置文件

$this->load->view('file_name', $data, true/false)

第三個可選參數使您可以更改函數的行為,以便它以字符串形式返回數據,而不是將其發送到瀏覽器。 如果您想以某種方式處理數據,這可能會很有用。 如果將參數設置為true(布爾值),它將返回數據。

然后在home視圖中,只需回顯$form

echo $form;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM