Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

MickaelAm's avatar

How to keep data on the same page after submitting a form ?

Hello,

I got a form page, when arriving on that page, I need to load some data for the user which is done in my controller's index function. So far so good.

When submitting the form, I want the user to be redirected to the same page but as I said I need to load data on that page when the user arrives on it.

Here's an example of what my controller looks like.

public function index()
{
    // retrieving data for the view's form
    $data = $this->setData(request('rid'));
    return view('payment.index', compact('data'));
}

public function store()
{
    // validations and saving form inputs
    // ...
    $response = 'Form success'; // and more

    // this is the problem
    // retrieving data again for the view's form
    $data = $this->setData(request('rid'));
    return view('payment.index', compact('data', 'response'));
}

Of course the current code works already, but is there a way to avoid code duplication here ?

Obviously, I don't need to fill $data twice, and I can omit sending $rid for the store function since I already used it in the index one.

Also, I can't simply redirect because some events will go on that page depending $response value.

There are too many data to store it session I think and I'd rather avoid using ajax here also.

1 like
2 replies
tisuchi's avatar
tisuchi
Best Answer
Level 70

What if you just use simply redirect()->back() in this case?

public function store()
{
    $response = 'Form success'; // and more

    return redirect()->back();
}
8 likes
MickaelAm's avatar

Indeed, that that was a dummy issue.

I totally forgot about that helper function. Thanks for reminding.

2 likes

Please or to participate in this conversation.