【发布时间】:2020-07-06 22:52:10
【问题描述】:
我有一个管理课程的页面,使用 CI 3 构建。当用户点击保存按钮时,表单将提交给 save 方法。我的问题是如何将用户重定向到上一页,并使用插入的数据填充表单?
目前发生的情况是,提交后,save 方法会加载 /new 页面的视图,并且 URL 保持为 /save 并填充数据。
已经尝试过重定向,但它丢失了用户插入的数据。
这里是我的保存方法的sn-p:
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/');
}
}
【问题讨论】:
-
您阅读了吗:Re-populating the form?它解释了如何使用提交的数据重新填充表单字段...
-
正在重新填充 for。那不是我的问题。问题是如何在保存功能中将用户重定向到表单,而不会丢失数据。如果我使用重定向,那么我会丢失数据。如果我加载视图,那么我会在表单中提供 /save url。我真正想要的是,在保存方法上验证表单。如果有效,插入数据库并重定向。那工作正常。如果没有,那么我希望用户使用已填充的数据重定向到 /new 视图。
-
重新填充表单是在解释如何重新填充表单,但表单是在同一个 url/function 上,而不是在处理逻辑的函数上
标签: forms codeigniter validation