2

I'm trying to redirect back two pages and have their input filled back in.

This is my sites flow:
1.Land on checkout page and fill it out. Hit submit.
2.Land on pin verification page where the customer enters their phone number to receive a pin code. They press submit.
3.They land on page where they enter the pin code that was sent to them.

They have 10 attempts to enter their pin code on the 3rd page. If it's incorrect after the 10th time I want to redirect back to the first page.

I know how to redirect back one page I can do \Redirect::back()->withInput()->withErrors(); but how can I redirect back two pages with an error displayed and the form filled out with the original values?

How can I do this?

MalcolmInTheCenter
  • 1,376
  • 6
  • 27
  • 47
  • Show your code what you have done so far. – common sense Jan 25 '18 at 18:59
  • @commonsense I don't have any because i'm completely lost with this. I know how to redirect back one page with `\Redirect::back()->withInput()->withErrors();` but have no idea how to go back twice and have the form filled in again with an error. – MalcolmInTheCenter Jan 25 '18 at 19:00

3 Answers3

0

Why not just redirect to() or route()? If the first page, i.e. the checkout page, you're trying to redirect to after failing all attempts is always the same it doesn't make sense to redirect twice, that's just adding an unnesseccary request.

  • I was thinking that but how can I fill out the form with the inputs the user added? – MalcolmInTheCenter Jan 25 '18 at 19:06
  • Same way as you have with `back()` the input and errors need to be kept in the session. You may need to call `save()` on the session prior to the redirect. –  Jan 25 '18 at 19:08
  • call `save()` on the session? what do you mean? I don't think i've ever done this. – MalcolmInTheCenter Jan 25 '18 at 19:10
  • The `Session` facade has a `save()` method if you need to access the value immediately. Otherwise, it won't be accessible until the start of the next request lifecycle. You may not need this, just a thought I had after reading another question regarding sessions. –  Jan 25 '18 at 19:15
0

Dont redirect back twice per se, that is, in pseudo-code dont attempt something like:

\Redirect::back()::back()

Largely because Laravel doesnt know the state / page 2 pages back. At best you can redirect back [once] based on HTTP_REFERER which is only the last URL visited.

Since you already know the page they should be redirected back too - why not just redirect directly to that URL?

I was thinking that but how can I fill out the form with the inputs the user added?

Store this somewhere: session, cookie, database, perhaps pass in the query string....

Cody Caughlan
  • 32,456
  • 5
  • 63
  • 68
0

You can use this solution to redirect two pages back. To pass the data add with(), withInput() and/or withErrors():

return redirect(session('links')[2])->withInput()->withErrors();

You'll be able to read this data the same way you would do this with back().

Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279