2014-04-04 58 views
0

我有一個方法在基本上添加一個用戶到一個表 - 如果任何表單驗證發生它重新加載視圖 - 如果成功它重新加載視圖,以顯示該用戶添加成功。如下圖所示:Codeigniter - 添加方法URL和重定向問題

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

我只給你一個例子,請安排根據保存和返回功能

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

    } 
} 
+0

似乎很大的成功,但如果表單驗證失敗呢? – TotalNewbie

+0

使用set_value或發回數據以查看並獲取 –

+0

檢查我更新的答案 –