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>