Welcome Guest, Not a member yet? Register   Sign In
redirect to same function
#1

[eluser]jon29[/eluser]
Hi sorry if this is a stupid question just want to check this isn't a bad practice,
In order that the flash data triggers at the next page load to confirm database had been updated, I need to use redirect to go back and reload the function, just checking that redirecting to the top of the same function isn't bad practice, code is below thanks


Code:
public function pages ()
  {
   if (isset($_POST['content_1'])){
    $this->load->library('form_validation');
    $this->form_validation->set_rules('content_1','content_1', 'trim|required');
    if($this->form_validation->run() == FALSE)
    {
    }
    else {
      $updated = $this->cms->update_pages();
      echo $updated;echo $updated;echo $updated;echo $updated;echo $updated;echo $updated;echo $updated;echo $updated;echo $updated;echo $updated;
      if (($updated == 1) ? $this->session->set_flashdata('Updated', 'Updated'): NULL);
      redirect ('admin/pages/'.$this->uri->segment(3));
    }
   }
  
  
   $this->load->helper('form');
   $data['style'] = array('css/admin/pages.css');
   $index = $this->uri->segment(3);
   $data['rows'] = $this->data_model->get_page($index);
   $this->load->view('admin/pages',$data);
  }
#2

[eluser]LynxCoder[/eluser]
[quote author="jon29" date="1334307985"]Hi sorry if this is a stupid question just want to check this isn't a bad practice,
In order that the flash data triggers at the next page load to confirm database had been updated, I need to use redirect to go back and reload the function, just checking that redirecting to the top of the same function isn't bad practice, code is below thanks
[/quote]

The biggest worry I'd have is the code getting stuck in the loop. Can you not have an external function that both the controllers of this page and the next can call to check the flashdata? That way you can handle what happens in the second controller, without any concerns of an endless loop.

Rich
#3

[eluser]jon29[/eluser]
thanks yes, that is the worry, but when uses redirect this seems to destroy the $_Post so it doesn't retrigger this part of the code, maybe it be safer to do the other way, unless someone knows that this is an accepted safe way to do things as it enable me to save code
#4

[eluser]LynxCoder[/eluser]
[quote author="jon29" date="1334321188"]thanks yes, that is the worry, but when uses redirect this seems to destroy the $_Post so it doesn't retrigger this part of the code, maybe it be safer to do the other way, unless someone knows that this is an accepted safe way to do things as it enable me to save code[/quote]

Yes the $_POST (you should use $this->input->post really) is only maintained for the page load immediately after the SUBMIT button is clicked, after that the content is lost.
#5

[eluser]jon29[/eluser]
ok thanks for help
#6

[eluser]boltsabre[/eluser]
You should do it like this:

1. Load controller/view which has your form.
2. User submits form and posts back to your controller
3. If validation fails, it posts back again. Repeat 2.
4. If validation passes, do everything you need to do with your $_POST array (ie, update DB via a model, set session stuff etc). Once finished, set your flashdata "confirmation messgae".
5. Redirect to a new controller to "activate" your flash, and display it to your user (this new controller can still display the same view as your controller in step 1, it doesn't matter. If it does, and the user submits the form again, the form will post back to your step 1 controller, starting the whole process again).

Code:
// controller
public function pages (){
   // all your stuff here
   // at the end, once you've finished with your post stuff, set your flash and redirect
   redirect('pages_confirmed');
}

public function pages_confirmed(){
   $this->load->view('.....');
}




Theme © iAndrew 2016 - Forum software by © MyBB