dongpochi9741 2014-04-04 09:48 采纳率: 100%
浏览 42
已采纳

Codeigniter - 添加方法URL和重定向问题

I have a method in CI which basically adds a user to a table - if any form validation occurs it reloads the view - if successful it reloads the view to show that the user was added successfully. As seen below:

public function loadPeopleView(){
    //loads unit page view
    $this->load->model('people_model');
    $people['people'] = $this->people_model->getPeople();
    $this->load->view("header");
    $this->load->view("people page/people_view", $people);
    $this->load->view("footer");
}

public function addPerson(){
    $this->form_validation->set_rules('personName', 'personName', 'required|min_length[6]|max_length[150]|trim|xss_clean');
    $this->form_validation->set_rules('personPet', 'personPet', 'required|trim|min_length[3]|max_length[30]|xss_clean');

    if($this->form_validation->run()){
        $this->load->model('');
        $this->people_model->addPerson();
        $this->loadPeopleView();
    } else{
        //if validation fails - returns the peopl view this display error messages
        $this->loadPeopleView();
    }
}

my issue is when someone adds a person the browser remains on: localhost/peoplecontroller/addperson

if the user keeps refreshing the page - loads of people will continue to be added in - is there anyway I can put the page back to: localhost/peoplecontroller/

without having to use a redirect as I still want any error messages from the form validation to appear

  • 写回答

1条回答 默认 最新

  • doudang4857 2014-04-04 09:55
    关注

    I am only giving you an example please arrange according save and return functionality

    public function addPerson(){
        $this->load->model('people_model'); // load model
       // validation
        $this->form_validation->set_rules('personName', 'personName', 'required|min_length[6]|max_length[150]|trim|xss_clean');
        $this->form_validation->set_rules('personPet', 'personPet', 'required|trim|min_length[3]|max_length[30]|xss_clean');
        // check validation not clear
        if ($this->form_validation->run() == FALSE) {
             //if validation fails - returns the peopl view this display error messages
             // also set error dat back
            // setting up send back values to view
             $this->data['personName'] = $this->input->post('personName');
            $this->data['personPet'] = $this->input->post('personPet');
           // get this->data values as a variable in view like $personName
            // load view
            $this->load->view("header");
            $this->load->view("people page/people_view", $this->data);
            $this->load->view("footer");
        } 
        else{ // after validation success
           // do your saving db stuff and set success message in session flash and redirect to
            $this->people_model->addPerson();
            // get and show message flash in your view
            $this->session->set_flashdata('message', 'Please check card details and try again');
            redirect('results', 'refresh');
    
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘