• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Redirect Back with Value

Is it possible to use redirect()->back(), with a value.    

I have a process that requires 2 forms.

Form 1 allows the user to define a date and passes it to Form 2

Form 2 retrieves data from the database (based on the date from Form 1) , allows the user to manipulate the data, then writes the result back to the database.

The problem is, I cannot redirect()->back() during the write process as I get cuaght in an infinite loop. I understand why I get caught in the loop, I'm just wondering if there is a way of passiing a value back with the redirect, so I avoid the infinite loop???

Any suggestions please

Form 1 - View
PHP Code:
<form action="Form2" method="POST" enctype="multipart/form-data">  
<label for="week_ending_search">Week Ending</label>
   <input type="text" name="week_ending_search" id="week_ending_search" data-date-format="dd-mm-yyyy" value="">
   <button type="submit" >Find</button>

Form 2 - Controller
PHP Code:
class Form2 extends BaseController {
    public function 
index() {
        //Check date is passed from Form 1        
if($this->request->getServer('REQUEST_METHOD') == 'POST'){   
            if (
$this->request->getGetPost('week_ending_search')) {                
redirect()->back()->withInput()->with('error'lang('Form2.dateNot Supplied'));
public function update() {
        //process Form 2
if($this->request->getServer('REQUEST_METHOD') == 'POST'){            
if($this->request->getPost('something')) {            
if(!empty($validation_errors)) {
                return redirect()->back()->withInput()->with('errors'$this->validation->listErrors());
if($writeTodatabase==false) {
                    return redirect()->back()->withInput()->with('error'lang('Form2.fail'));  //GETS CAUGHT IN INFINITE LOOP 
                    return redirect()->back()->withInput()->with('errors'$this->validation->listErrors());  //GETS CAUGHT IN INFINITE LOOP 
redirect()->back()->withInput()->with('error'lang('Form2.invalidUpdate'));  //GETS CAUGHT IN INFINITE LOOP 

Form 2 - View
PHP Code:
<form action="Form2/update" method="POST" enctype="multipart/form-data">  
<label for="toDo">To Do</label>
   <?php foreach($toDo as $mylist) : ?>
   <input type="text" name="toDo[]" value="<?= esc($mylist?>">
   <?php endforeach ?>
   <button type="submit" >Update</button>

You can try this it uses the old in the froms.

PHP Code:
// Keep the old input values upon redirect so they can be used by the `old()` function
return redirect()->back()->withInput(); 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )

Thank you.

I use this all the time, and it works very well. The issue is, that when

return redirect()->back()->withInput(); 
is called,  the it seems the controller is reloaded and this obviously calls the index function, and my index function is expecting a server POST (or GET), so I get caught in an infinite loop.

Is there a way of passing the original variable in the redirect so my controller can process?

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

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