vm's avatar
Level 2

form submitt callback

When i submit a form (say image upload) everything works fine and the file is uploaded and saved. However if I reload the page it submits the form again. How do I prevent this? Also is there a callback after the upload is done -- intent being to show a spinner or progress while the file is being loaded. thx

0 likes
5 replies
BENderIsGr8te's avatar

What does your controller look like? For example, if you are trying to return a view instead of a redirect this problem would exist.

Here's an example of what would cause that problem

public function processForm(Request $request)
{
    // Do form stuff here...
    // still processing all your form stuff....
    // Form processing stuff is done...

    return view('successpage');
}

In the example above the problem is that while the URL may look fine, you are on a "POST" route, so hitting reload is reloading the POST request, not a GET request, which Laravel correctly routes to the POST form controller.

Here's an example that shouldn't cause the problem.

public function processForm(Request $request)
{
    // Do form stuff here...
    // still processing all your form stuff....
    // Form processing stuff is done...

    return redirect()->back();
}

Or alternatively you could redirect to a different page. The back() function redirects them back to the GET request that loaded the form, so hitting reload would then reload the GET request and not the POST request.

absiddiqueLive's avatar

If your browser have post/get request and then if you reload it will submit your request it'e natural for php. So, to avoid this problem you needs to redirect other page after successfully process your data, so that the request died from browser, and it's the normal and easiest way.

Or can manage the request manually .

vm's avatar
Level 2

@BENderIsGr8te & @absiddiqueLive

Thx, that is the problem -- My original view is a table showing a list of images and I was returning a view with the new item added - I am still on 4.2 -- not switched yet, on the list -- so there is no redirect back call -- I tried Redirect::to(myoriginalgetaddress) and Redirect::route(myoriginalgetroute). But both did not work -- just gives a blank screen with the post address showing. any thoughts -- thx

BENderIsGr8te's avatar

L5 was my first version of Laravel. But it looks like in 4.2 you should be able to do something like...

return Redirect::back();

Make sure you are returning the Redirect (to be safe). The 'back' refers to the page that they came from.

vm's avatar
vm
OP
Best Answer
Level 2

Well Redirect::back() did not work either, it prevented from posting again. However I got a blank screen with the post address in the title. What finally worked is (in case anybody else faces this situation)

return Redirect::to(URL::previous());

Please or to participate in this conversation.