Welcome Guest, Not a member yet? Register   Sign In
Controller flow when processing forms
#1

[eluser]vendiddy[/eluser]
Hi all! Is there a standard way that form processing is implemented in CodeIgniter? Here are the high-level steps I have in mind:

1. Display empty form
2. Submit
A. If there are validation errors, redisplay the form with the errors.
B. If there are no validation errors, display a success page.

How would I set up my controller methods? Would I have one method to display the view and another method to process the view? (Or would both go in the same method?)

Btw, I already know the basics of CodeIgniter (including the validation library).

Thanks!
#2

[eluser]Aken[/eluser]
To put it simply without code:

1. Set validation rules and field names.
2. Check to see if the form validates.
3. If false (also the default), display the form w/ any errors.
4. If true, process the form then display a message, redirect the page, or whatever you want.
#3

[eluser]Mirage[/eluser]
Here's default skeleton for a controller function that processes a form. I tend to post and get to the same action:

Code:
function contactus() {

    if($_SERVER['REQUEST_METHOD']=='POST'){
        $this->load->library('Validation');
        $this->load->config('cfg_forms');

        $this->validation->set_fields($this->config->item('form_contactus_fields'));
        $this->validation->set_rules($this->config->item('form_contactus_rules'));
        $this->validation->set_error_delimiters('<div>','</div>');

        if ($this->validation->run()) {
                // Do stuff with the form data

                // Choose to redirect here or show the same form again
        }

    }

    $this->load->view('contactus');

}

HTH
-m
#4

[eluser]vendiddy[/eluser]
Thanks Aken & Mirage! That really cleared things up.




Theme © iAndrew 2016 - Forum software by © MyBB