【问题标题】:Codeigniter - add method URL & Redirect issueCodeigniter - 添加方法 URL 和重定向问题
【发布时间】:2014-04-04 09:48:30
【问题描述】:

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

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

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

【问题讨论】:

    标签: php codeigniter url-redirection


    【解决方案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');
    
        }
    }
    

    【讨论】:

    • 看起来很成功,但如果表单验证失败怎么办?
    • 使用set_value或者发回数据查看获取
    • 这个答案对你有帮助吗?
    猜你喜欢
    • 2015-04-15
    • 2015-11-12
    • 2016-07-02
    • 2011-09-25
    • 2012-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多