dongqian5384 2016-05-30 11:43
浏览 38

codeigniter中的URL重定向

I'm creating php system using codeigniter framework. I'm using basic framework without any templates. When I submit data from html page to controller to save to db after redirecting with the error message to view file, i can't remove the url that i used to save data in controller file. How I solve this problem?

This is the form i used to send data to controller.

<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>

This is the controller function i called.

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();
         }
}

Please Help me to solve this problem.

  • 写回答

3条回答 默认 最新

  • dongtuojuan8998 2016-05-30 12:58
    关注

    You will need to redirect to a new controller/function in order to get the URL to change in the browser. I'm going to call the controller control and add a couple functions to handle success and failure.

    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');
    }
    

    Another option for presenting a failed attempt is to use sessions to pass an error message back to the controller/function that loads the form and redirect to that page instead of the save_fail page.

    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器