CodeIgniter Forums
Processing forms - redirecting/passing to controllers - Printable Version

+- CodeIgniter Forums (
+-- Forum: Using CodeIgniter (
+--- Forum: Model-View-Controller (
+--- Thread: Processing forms - redirecting/passing to controllers (/showthread.php?tid=69937)

Processing forms - redirecting/passing to controllers - vundo - 02-01-2018


I have created a controller, let's say Customer. I'm going to change some data in database using values passed by form.

The method index() contains code to load view files, where form is generated using form helper (because I've enabled CSRF protection).

The head of HTML form is
PHP Code:
which generates
<form ... action="http://localhost/customer/change_password" method="POST" />
Of course I have created a method called change_password, where I can get the data passed from HTML form, but first I check in this method if the form is validated (contains all required fields etc.)

The problem is, that the website loads pages in this sequence:
index -> user fills the form and submits -> redirects to change_password -> redirects to index

How can I process the form in much more elegant way? I mean:
index -> filling and submitting the form -> reload to index with status message

The forms in CI are a nightmare for me Sad. Here is my change_password
PHP Code:
public function change_password()
       $this->form_validation->set_rules('old_password''Old password''required');
       $this->form_validation->set_rules('new_password''New password''required|min_length[' $this->config->item('min_password_length''ion_auth') . ']|max_length[' $this->config->item('max_password_length''ion_auth') . ']|matches[new_password_confirm]');
       $this->form_validation->set_rules('new_password_confirm''Powtórz nowe hasło''required');
       $user $this->ion_auth->user()->row();
       if ($this->form_validation->run() === false) {
           redirect('customer''refresh'); // FIRST
       } else {
           $identity $this->session->userdata('identity');
           $change $this->ion_auth->change_password($identity$this->input->post('old_password'), $this->input->post('new_password'));
       if ($change) {
           //if the password was successfully changed
           redirect('customer''refresh'); //SECOND
       } else {
           redirect('customer''refresh'); //THIRD

RE: Processing forms - redirecting/passing to controllers - Elias - 02-01-2018


You can check passing 'submit' to index() method. If form was submitted, value of submit button will be passed to your controller.