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

时间:2014-04-04 09:48:30

标签: php codeigniter url-redirection

我在CI中有一个方法,基本上将用户添加到表中 - 如果发生任何表单验证,则重新加载视图 - 如果成功,则重新加载视图以显示用户已成功添加。如下所示:

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

我的问题是当有人添加浏览器的人时: 本地主机/把PeopleController / AddPerson时

如果用户不断刷新页面 - 将继续添加人员 - 无论如何我可以将页面放回: 本地主机/把PeopleController /

无需使用重定向,因为我仍然希望表单验证中出现任何错误消息

1 个答案:

答案 0 :(得分:1)

我只给你一个例子,请根据保存和返回功能进行安排

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

    }
}