forms - 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/');
}
}
解决方案
要重新填充表单,您必须使用字段中的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表单验证。
推荐阅读
- linux - Linux bash脚本获取自己的互联网IP地址
- javascript - Flatlist 不会填满整个屏幕
- iis - IIS 如何识别请求的站点?
- qliksense - 如何在 Qlik Sense 中将标签放置到分层维度
- typescript - 如何对 PDFTron 中的现有数据元素执行 click()?
- python - paramiko 不在机器上执行基本命令
- reactjs - React-hot-loader:`hot`找不到你提供的`module`的`name`
- c# - 在 C# 中加密的 OpenSSL PHP 中解密 AES 消息
- php - Woocommerce 产品过滤 wp 查询
- javascript - 响应总是整个 ajax 数据包