【问题标题】:Codeigniter How to redirect back to previous page without losing dataCodeigniter 如何在不丢失数据的情况下重定向回上一页
【发布时间】:2016-01-19 09:17:13
【问题描述】:

这是我的项目控制器,它显示项目列表,并从 items_view.php 中查看。

class Items extends CI_Controller{

function  __construct(){
    parent::__construct();
    $this->load->model('crud');
}
function index(){
    $data['items'] = $this->crud->read('items');
    $data['header_info'] = $this->crud->header_info('items');
    #$data['single_row'] = $this->crud->read_single('items','ID', 1);
    $this->load->view('items_view', $data);
}

function edit($id){
    $data['single_row'] = $this->crud->read_single('items','ID', $id);
    $this->load->view('items_view', $data);
}

function insert(){
    if($_POST){
        $data = array(
        'Desc' => $_POST['Desc'],
        'Cost' => $_POST['Cost']        
        );
    if($_POST['status']=='update'){
        $this->crud->update('items', $data, 'ID',$_POST['id']);
        echo "Updated...";
    }elseif ($_POST['status']=='new'){
        $last_row = $this->crud->insert('items', $data);
        if($last_row){
            #Data insertion completed.....
            #now ready to get back my items list page.....!
        }   
    }


    }
  }
}

在 items_view.php 中也有表单可以让用户向列表中添加更多项目,所以我想,当用户提交表单时,insert 方法将执行,所以如何回到我的上一个页面而不丢失数据。

【问题讨论】:

  • 在表单提交时,您是否将值存储在 db 中??
  • @Niranjan N Raju 是的,我存储了它,但是回到上一页很困惑......
  • 在编辑页面中,编辑后,你想回到相同的编辑页面,所有输入的值??
  • 一开始用户会看到从数据库中提取的项目列表,用户也会使用空白表单来添加更多项目,所以如果用户填写该表单并说保存,表单将执行项目控制器->insert 方法,插入完成后,我需要在上一页向用户显示一个包含新项目的列表,所以我需要再次调用 index 方法吗?
  • 在索引中你有表格和表格吗?

标签: php codeigniter url redirect


【解决方案1】:

insert() 中,如果插入行或更新行,则获取 id 然后像这样重定向到index()

redirect("items/index/".$last_row);

index()

function index($id = ""){
    if($id) { 
        // fetch data from db and pass it to view
    } else {
        // pass empty value to view
    }
    $data['items'] = $this->crud->read('items');
    $data['header_info'] = $this->crud->header_info('items');
    $this->load->view('items_view', $data);


}

【讨论】:

    猜你喜欢
    • 2021-05-18
    • 2020-03-20
    • 2023-04-05
    • 1970-01-01
    • 2018-07-15
    • 2021-09-03
    • 1970-01-01
    • 2019-01-11
    • 1970-01-01
    相关资源
    最近更新 更多