2

I want to use for my form two submit buttons, the first one is to save and redirects you to the homepage and the second one is to save and redirects you to the same form again, I use public function store to save it, here is my form

   <form method="post" action="{{route('return.store')}}" enctype="multipart/form-data">
   <div class="form-group">
   <button type="submit" class="btn btn-md" name="submit">Verstuur</button>
   </div>
   </form>

And my controller

public function store(Request $request)
    {
        $this->validate($request, [
            'image' => 'image|nullable|max:1999'
        ]);

        if ($request->hasFile('image')) {
            $filenameWithExt = $request->file('image')->getClientOriginalExtension();
            $filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
            $extension = $request->file('image')->getClientOriginalExtension();
            $fileNameToStore = $filename . '_' . Carbon::today()->toDateString() . '.' . $extension;
            $request->file('image')->storeAs('public/images', $fileNameToStore);
        } else {
            $fileNameToStore = 'noimage.jpg';
        }

        $retour = new Retour();

        $retour->firmaname = request('firmaname');
        $retour->contactperson = request('contactperson');
        $retour->email = request('email');
        $retour->ordernumber = 653 .request('ordernumber');
        $retour->articlenumber = request('articlenumber');
        $retour->return_quantity = request('return_quantity');
        $retour->return_quality = request('return_quality');
        $retour->return_reason = request('return_reason');
        $retour->images = $fileNameToStore;

        $retour->save();

        return redirect('/return')->with('message', 'Je retourmelding is succesvol verzonden');
    }

Othman Boūlal
  • 81
  • 2
  • 10

2 Answers2

4

you can use name attributes with different values in your submit buttons, and then perform the intended action based on the value:

<form method="post" action="{{route('return.store')}}" enctype="multipart/form-data">
    <button type="submit" name="action" value="homepage">Save</button>
    <button type="submit" name="action" value="same_form">Save</button>
</form>

In the controller, you can do as like this,

public function store(Request $request)
{
    if($request->action == 'homepage'){
         return redirect('homepageurl');
    }    
    if($request->action == 'same_form'){
         return redirect()->back();
    }
}

Check this content if you stuck anywhere.

Dilip Hirapara
  • 14,810
  • 3
  • 27
  • 49
1

You have to handel request by submit button name.

<form method="post" action="{{route('return.store')}}" enctype="multipart/form-data">
    <div class="form-group">
         <button type="submit" class="btn btn-md" name="submit">Verstuur</button>
    </div>
    <div class="form-group">
         <button type="submit" class="btn btn-md" name="return">return</button>
    </div>
 </form>

And then in the controller:

if($request->submit){
    return view('Homepage')->with('message', 'Saved');
} else {
     return redirect('/return')->with('message', 'Je retourmelding is succesvol verzonden');

}
Amit Senjaliya
  • 2,867
  • 1
  • 10
  • 24