Welcome Guest, Not a member yet? Register   Sign In
Prepopulating form data
#2

[eluser]TheFuzzy0ne[/eluser]
This is how I usually do it:
Code:
class Homepage extends Controller {

...

function add_something()
{

    // We need the session class for flashdata and url helper
    // for redirects.
    $this->load->library('session');
    $this->load->helper('url');

    // Load the validation class. This will allow you to use
    // set_value() in your view. Use the second parameter
    // of set_value() to populate the form with the data
    // from the database for the first time.
    $this->load->library('form_validation');

    // See if the form is being submitted
    if ($this->input->post('submit'))
    {
        // Run the validation (set up the rules first)
        if ($this->form_validation->run() === TRUE)
        {
            // set a flashdata message
            $this->session->set_flashdata('message', 'Entry successfully added!');
            
            // Redirect to another page
            redirect('/some_controller');
        }
    }
    // Set an array to feed into the view
    $data['form_data'] = $this->some_model->getFormData();

    // Load the default view
    $this->load->view('default_view', $data);
}

}

Basically, the controller will keep loading so long as the validation fails. When the validation is successful, the user is redirected, which prevents them accidentally resubmitting the form when using the back button, or refreshing a page.

Ceveat: This method relies on cookies to keep the user posted, although it's possible to do it without the need for cookies easily enough.


I hope this helps.


Messages In This Thread
Prepopulating form data - by El Forum - 02-09-2009, 06:02 PM
Prepopulating form data - by El Forum - 02-09-2009, 06:16 PM
Prepopulating form data - by El Forum - 03-02-2009, 04:39 PM
Prepopulating form data - by El Forum - 03-02-2009, 06:08 PM
Prepopulating form data - by El Forum - 03-02-2009, 07:56 PM
Prepopulating form data - by El Forum - 03-02-2009, 08:10 PM
Prepopulating form data - by El Forum - 03-06-2009, 05:06 AM



Theme © iAndrew 2016 - Forum software by © MyBB