• 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Processing forms - redirecting/passing to controllers

#1
Hello!

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:
form_open('customer/change_password'); 
which generates
Code:
<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) {
 
           $this->session->set_flashdata('message'validation_errors());
 
           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
 
           $this->session->set_flashdata('message'$this->ion_auth->messages());
 
           redirect('customer''refresh'); //SECOND
 
       } else {
 
           $this->session->set_flashdata('message'$this->ion_auth->errors());
 
           redirect('customer''refresh'); //THIRD
 
       }
 
   
Reply

#2
Hello!

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


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2018 MyBB Group.