首页 > 解决方案 > CodeIgniter - 保存方法和重定向

问题描述

我有一个管理课程的页面,使用 CI 3 构建。当用户点击保存按钮时,表单将提交给该save方法。我的问题是如何将用户重定向到上一页,并使用插入的数据填充表单?

目前发生的事情是提交后,save 方法加载/new页面的视图,并且 URL/save与填充的数据保持一致。

已经尝试过重定向,但它会丢失用户插入的数据。

这是我的保存方法的片段:

public function save()
{
    $this->checkIsAdmin();
    $this->load->helper('form');
    $this->load->library('form_validation');

    $this->form_validation->set_rules('name', $this->lang->line('common_name'), 'required');
    if ($this->form_validation->run() === FALSE)
    {
        $this->feedback_message->create('Please check fields', 'error');
        $this->baseInfo['page_title'] =  "Registar novo curso";
        $this->mybreadcrumb->add('Registar curso', base_url('course/new'));
        $data['schools'] = $this->School_model->listSchools();

        $this->load->view('web/top', $this->baseInfo);
        $this->load->view($this->templatesFolder.'tabs_start', array('courseId' => 0));
        $this->load->view($this->templatesFolder.'manage', $data);
        $this->load->view($this->templatesFolder.'tabs_end');
        $this->load->view('web/footer');

        /*redirect("/course/new", "location");*/
    }else{
        $saved = $this->Course_model->saveCourse();
        if($saved){
            $this->feedback_message->create('Success', 'success');
        }else{
            $this->feedback_message->create('Error '.$_POST['name'], 'error');
        }
        redirect('course/');
    }
}

标签: formscodeignitervalidation

解决方案


要重新填充表单,您必须使用字段中的set_value('field_name')函数,input如下所示:

<input type="text" name="name" value="<?php echo set_value('name'); ?>" />

将之前的视图返回为:

$this->load->view('view_name',$All_Data_For_View);

仍然如果您不理解,请再次阅读 CI表单验证


推荐阅读