简体   繁体   中英

Codeigniter controller functions

In my controller i have a view for registering new members.

Instead of making a whole new function for that such as,

public function show_registration(){
$this->load->view('registration');
}

public function validate_registration(){

}

Instead of doing the one above where i make a function to show the view then a new one to receive the data from the view, i made it like this.

public function registration(){
if(array_key_exists('submit',$_POST)){
/////if exist process the data here
}else
$this->load->view('registration');
}

The above if statement, checks if ever there was data submitted to the function if not then it would show the view else it will process the data.

It seems like more of an ideal way right? then i thought, what if this could cause problems?

Is there any chance or possibility this might ruin my project? I am starting right now, and i just want insights whether i should pursue my current way or i should just create a separate one for showing the view.

**EDIT: I just realized, this does not necessarily check all submitted data, but only data submitted by the button of the name 'submit', so as long as i keep the name of the submit button for every form unique as possible then i should be fine.

sample: form for registration, button name should be registration.**

Here is an example of what you can do with codeigniter

class Form extends CI_Controller {

    function index()
    {
        $this->load->helper(array('form', 'url'));

        $this->load->library('form_validation');

        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('myform');
        }
        else
        {
            $this->load->view('formsuccess');
        }
    }
}

使用Codeigniter,您必须遵循codeigniter-form-validation来加载视图或提交表单以获取更多信息,请访问此Codeigniter form_validation库

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM