[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.