![]() |
redirect to same function - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: redirect to same function (/showthread.php?tid=50913) |
redirect to same function - El Forum - 04-13-2012 [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 () redirect to same function - El Forum - 04-13-2012 [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 redirect to same function - El Forum - 04-13-2012 [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 redirect to same function - El Forum - 04-13-2012 [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. redirect to same function - El Forum - 04-13-2012 [eluser]jon29[/eluser] ok thanks for help redirect to same function - El Forum - 04-13-2012 [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 |