codeigniter中的URL重定向

时间:2016-05-30 11:43:07

标签: php codeigniter

我正在使用codeigniter框架创建php系统。我正在使用没有任何模板的基本框架。当我将数据从html页面提交给控制器以便在重定向并查看错误消息后保存到数据库时,我无法删除用于在控制器文件中保存数据的URL。我是如何解决这个问题的?

这是我用来向控制器发送数据的形式。

<form role="form" action="save_user_type" method="POST" >
<div class="row">

            <div class="form-group col-md-6">
              <label>User Type</label>
              <input type="text" class="form-control" name="type" id="type" placeholder="Enter ...">
            </div>
            </div>

            <div class="row">
                <div class="form-group col-md-6"></div>
                <div class="form-group col-md-6">
                   <button type="submit" class="btn default">Save</button> <button type="button" class="btn default">Reset</button>
                </div>
            </div>
          </form>

这是我调用的控制器功能。

function save_user_type(){
    try {
        $this->load->model('Configuration/Configuration_model');
        if($this->Configuration_model->save_types() == 1){
            $success['done'] = "Good Job";
        }else{
            $success['dont'] = "Error";
        }
        $this->load->view('template/header');
        $this->load->view('template/menubar');
        $this->load->view('configuration/user/index',$success);

    } catch(Exception $exc){
             return $exc->getTraceAsString();
         }
}

请帮我解决这个问题。

3 个答案:

答案 0 :(得分:0)

您需要重定向到新的控制器/功能才能在浏览器中更改URL。我将调用控制器control并添加几个函数来处理成功和失败。

public function save_user_type(){
    try {
        $this->load->model('Configuration/Configuration_model');
        if($this->Configuration_model->save_types() == 1){
            redirect('control/save_success');
        }else{
            redirect('control/save_fail');
        }
    } catch(Exception $exc){
         return $exc->getTraceAsString();
      }
}

public function save_sucess(){
   $this->load->view('template/header');
   $this->load->view('template/menubar');
   $this->load->view('configuration/user/index');
}

public function save_fail(){
   $this->load->view('template/header');
   $this->load->view('template/menubar');
   //the "fail" view will have the error message 
   //and maybe a link back to the page with the form.
   $this->load->view('configuration/user/fail');
}

尝试失败的另一个选择是使用会话将错误消息传递回加载表单的控制器/函数,并重定向到该页面而不是save_fail页面。

答案 1 :(得分:0)

我认为您需要在提交数据后重定向到索引页面。 您可以使用此代替加载视图页面。

redirect('controller_name/function_name');

答案 2 :(得分:0)

使用flashdata显示消息并重定向到另一个页面。

function save_user_type(){
    try {
        $this->load->library('session');
        $this->load->model('Configuration/Configuration_model');
        if($this->Configuration_model->save_types() == 1){
            $this->session->set_flashdata('success',"Good Job");
            redirect('controller_name/function_name');
        }else{
            $this->session->set_flashdata('error',"Error Occured");
            redirect('controller_name/function_name');
        }
        $this->load->view('template/header');
        $this->load->view('template/menubar');
        $this->load->view('configuration/user/index',$success);

    } catch(Exception $exc){
             return $exc->getTraceAsString();
         }
}

在视图上显示消息使用$this->session->flashdata('success');表示成功,$this->session->flashdata('error');表示错误