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

Ligonsker's avatar

I should not return a view from a POST request, but how to make it display the data on the same page?

I am POSTing some data from /some_url to a route, and from there to the Controller to calculate the data. The Controller needs to return the calculated data (Currently saved in compact($var, $vars)).

What is the proper way to return this data to the same /some_url without using JS?

0 likes
9 replies
Ligonsker's avatar

@MichalOravec I tried to use return redirect(some_url), but because the initial submit was a POST, it goes to an infinite loop? (It tries to resubmit the form again and it redirects to himself)

Sinnbeck's avatar

@Ligonsker if it goes into an infinite loop we are missing some information. Is it redirected to a page that automatically calls the post route?

Sinnbeck's avatar

Why is it calculated there in the first place, if all its meant to user for is being returned? Or do you save it in the database as well?

1 like
Ligonsker's avatar

@Sinnbeck Nope, just bad legacy code I'm working on with a lot of bad practice. And it would be easier to just leave it as is right now 😅

Sinnbeck's avatar

@Ligonsker Ok. Well if it isnt used for anything, you grab some identifier (an ID perhaps) and pass it to a get route (or store it in session). Then in that get route you add the calculations, and return a view.

Ligonsker's avatar

@Sinnbeck Why is it different than what's being done now?

Currently, the form submits to a Controller then the Controller returns a view (Although this view is the same view)

Also, why is it bad to return a view from POST? To prevent some re-submission loops?

Sinnbeck's avatar

@Ligonsker Because POST requests are made to store data. So if I end up on a post request and hit F5, I will store the same data again

1 like
Ligonsker's avatar

@Sinnbeck thank you! that's exactly what's happening right now (It keeps fetching that data on F5 after the first submit even though I want it only on button click)

Please or to participate in this conversation.