代码点火器中的多个表单视图

时间:2012-03-18 21:13:38

标签: php forms model-view-controller codeigniter

我有一个名为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

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

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

2 个答案:

答案 0 :(得分:4)

你可以这样做:

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');
}

}

答案 1 :(得分:0)

您还可以将重定向功能用于加载第二种形式的控制器 例如

//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
    }

等。希望这会有所帮助。