【问题标题】:Multiple forms views in code ignitercodeigniter 中的多个表单视图
【发布时间】:2012-04-03 11:25:16
【问题描述】:

我有一个名为 Author 的控制器。在控制器内部,我定义了一个提交函数,负责处理 3 步表单过程。现在我只创建了第一个表单。

class Author extends CI_Controller {

    public function index() {
    }

    public function submit() {  
        $this->load->view('author/submit_step1');
    }

}

视图中加载的form1指向URL(动作)

http://localhost/zabjournal/Author/submit/2

我的目标是将第一个表单的值保存到数据库中,然后加载第二个表单。

鉴于表单的动作,我应该如何设计控制器以便我可以访问模型并保存第一个表单的详细信息,然后为第二个表单加载视图。

【问题讨论】:

  • 您需要在存储每个step-form-values的会话中注册一个表单模型。由于您还想将该模型保存到数据库中,因此请执行此操作。

标签: php forms model-view-controller codeigniter


【解决方案1】:

你可以这样做:

public function submit (){

if ($this->input->post('submit-1')) //Use unique names for each submit button value on the forms
{
    //validate and save form data to database

    $this->load->view('author/submit_step2');
}
elseif ($this->input->post('submit-2'))
{
    //validate and save form data to database

    $this->load->view ('author/submit_step3');
}
elseif ($this->input->post('submit-3'))
{
    //validate and save form data to database

    $this->load->view('author/success');
}
else
{
    $this->load->view('author/submit_step1');
}

}

【讨论】:

  • 为了使其与问题中提供的 URL 更兼容,submit() 应该期望一个变量,例如 $step,您在 if 语句中使用该变量,而不是 post 变量。
【解决方案2】:

您还可以使用重定向功能到加载第二个表单的控制器。
例如

//the function that loads the first form<br><br>
public function load_firstForm() {  
        $this->load->view('author/form1');
    }

//in your form1 use form_open('author/submit_step1') to access the second function

public function submit_step1() {
        //load your model here and a method to save these items

      //redirect to the same controller but the second method that loads the second form
       redirect('author/submit_step2');
    }

//function that loads form2

public function submit_step2() {  
        $this->load->view('author/form2');//loads the second form
    }

等等。希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2012-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多