0

I have a form that sends data via POST to a controller function which displays a view with overview of what the user put in. I this view user has 2 options - SAVE the data if they are OK or EDIT them.

Is there some way to redirect back with the input? I cant figure it out so Im using a form in the view with hidden input which carries the data a then via POST method send it to the display form function. But I don't think that is the right way there should be something simpler.

Thank you

vikiedr
  • 9
  • 1
  • 7

4 Answers4

1

Assuming you are using Laravel 5.5

After reading the comments on Nisarg Shah's answer i think what i would do to not have the hidden form, is to save the data he first passes in a session and after the final save(After edit or pressing the save button) just remove the data from the session.

So after the first form we arrive at your controller method, in here we save the data to a session and shows the user a view where he can see the data, here you would print the data with {{ session('key') }} somewhere in the view for all the data.

The user can now choose to edit the data, the edit button would then go to a specific route and here you would return a view with a form he could edit, and after edit post to another route where you would just save the data as normal.

If he presses save on the "confirmation" view you would again make the button go to a specific url and in the controller method just retrieve the session data and save it to the database.

coleander
  • 551
  • 6
  • 10
0

Have you tried this..?

public function store()
{
   //.....    

   return redirect()->back()->withInput();  
}
Abid Raza
  • 745
  • 8
  • 15
0

You can use

back()->withInput();

or if you want to redirect to a another URL use

return Redirect::route('ROUTE.NAME')->withInput();

Manoj H L
  • 843
  • 9
  • 22
0

So heres what your going to do:

  • Lets say the user wants to save the data and clicks okay, you can simply store this information into a database through a model or through query builder

    DB::table('usersinput')->insert([
       ['email' => 'taylor@example.com', 'name' => 'taylor'],
       ['email' => 'dayle@example.com', 'name' => 'dayle']
    ]);
    
  • Lets say the user clicks edit, than you can return the view with the values

    $email = "dayle@example.com";
    $name = "dayle";
    return view('welcome', compact('email', 'name'));
    
  • in the view you can set the input value to the data

    <form>
      email:<br>
      <input type="text" name="{{ $email }}"><br>
      name:<br>
      <input type="text" name="{{ $name }}">
    </form>
    
Nisarg Shah
  • 282
  • 2
  • 6
  • 14
  • Well Im not sure about this ... how can I call "return view('welcome', compact('email', 'name'));" when Im in a view. Or maybe Im just missing something – vikiedr Nov 08 '17 at 15:56
  • you call "return view('welcome', compact('email', 'name'));" through the controller. – Nisarg Shah Nov 08 '17 at 15:58
  • but how ? In the controller I receive the data from the form and display them in the view .... so Im not in the controller anymore and am displaying the view to the user who can click SAVE or EDIT. so there has to be for example to lead to another controller method or something. right ? – vikiedr Nov 08 '17 at 16:01
  • when you click edit does it go into another controller or view? – Nisarg Shah Nov 08 '17 at 16:02
  • now Im doing it like this: in the view theres a hidden form which holds all the data and when you click SAVE or EDIT it sends the data through POST request to a controller. Which handles the data and display a view. – vikiedr Nov 08 '17 at 16:05
  • from that controller you can pass the data to the view with the compact function as shown above and display the data as a value in the input tag(if you already checked if user clicked edit) – Nisarg Shah Nov 08 '17 at 16:08
  • yeah but that way I still have to have the hidden form and send the data via POST request to that controller which will than show the edit form right? I was hoping for some solution where I could get rid of the hidden form and pass the data more sofisticated – vikiedr Nov 08 '17 at 16:13
  • Okay lets have it so when a person enters the information it will directly go into the database, now he has the option to edit or leave the information alone, if he edits we can simply update the row. – Nisarg Shah Nov 08 '17 at 16:16
  • I thought of that too. But if he for some reason decides to not continue and just close the page than we have a saved record of that in our database even thou he didn't confirm the form. @coleander suggested to use session for that and it sounds good. But thanks anyway :) – vikiedr Nov 08 '17 at 18:05