Welcome Guest, Not a member yet? Register   Sign In
Redirect Back with Value
#1

Hi,
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

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'){   
            helper
(['form''url']);
            if (
$this->request->getGetPost('week_ending_search')) {                
                
$data['toDo']="GET TODO FROM DATABASE";
            }else{
                return 
redirect()->back()->withInput()->with('error'lang('Form2.required'));
            }
        }else{
            return 
redirect()->back()->withInput()->with('error'lang('Form2.dateNot Supplied'));
        }
        
        echo 
view('Form2'$data);
    }
    
    
    
    
public function update() {
        
        //process Form 2
        
if($this->request->getServer('REQUEST_METHOD') == 'POST'){            
            
helper(['form''url']);
            
            
if($this->request->getPost('something')) {            
                $data['something']=$this->request->getPost('something'FILTER_SANITIZE_STRING);
            }
            
            $this
->validation->reset();
            $this->validation->run($data'validation');               
            $validation_errors
=$this->validation->getErrors();
            
            
if(!empty($validation_errors)) {
                return redirect()->back()->withInput()->with('errors'$this->validation->listErrors());
            }else{
                $writeToDatabase="dosomething";
                
                
if($writeTodatabase==false) {
                    return redirect()->back()->withInput()->with('error'lang('Form2.fail'));  //GETS CAUGHT IN INFINITE LOOP 
                }else{
                    return redirect()->back()->withInput()->with('errors'$this->validation->listErrors());  //GETS CAUGHT IN INFINITE LOOP 
                }
            }
        }else{
            return 
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>
</form> 
Reply
#2

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 )
Reply
#3

Thank you.

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

Code:
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?
Reply
#4

"68thorby68" Are you solved this issue ? I have simmilar problem. I have one FORM in step 1 and some validation there. It's working fine. But when user go to Step 2 form and I want to make some validation there I cant. If I use route_to it will return to step 1 instead to step 2 and with back method it's infinite loop as you described.
Reply
#5

(01-24-2022, 07:05 AM)jozefrebjak Wrote: "68thorby68" Are you solved this issue ? I have simmilar problem. I have one FORM in step 1 and some validation there. It's working fine. But when user go to Step 2 form and I want to make some validation there I cant. If I use route_to it will return to step 1 instead to step 2 and with back method it's infinite loop as you described.

Maybe you can manipulate the reason of the loop. Is there something in the request like checking the button which was clicked, or a hidden filed?

Or you can stop the loop by check for the error-data?

Just some ideas ;-)
Reply




Theme © iAndrew 2016 - Forum software by © MyBB