Laravel 5.1 one form two submit buttons

I am using Laravel 5.1, and I would like to create a form with two submit buttons - Save and save the draft.

But when I submit my form, I have all the fields except the submit value.

I read that Laravel will not put the value of the submit button in POST when the form was submitted via ajax, so that you can help me how to do this?

I tried the code as below:

{!! Form::open(['url' => 'offer/create', 'method' => 'post', 'id' => 'offer-create']) !!} .... here are my fields .... {!! Form::submit( 'Save', ['class' => 'btn btn-default', 'name' => 'save']) !!} {!! Form::submit( 'Save draft', ['class' => 'btn btn-default', 'name' => 'save-draft']) !!} 

In my .php routes, I have:

 Route::controller('offer', 'OfferController'); 

Thank you in advance

+8
source share
3 answers

you can use the same name and different value attribute for submit buttons

//example:

 {!! Form::submit( 'Save', ['class' => 'btn btn-default', 'name' => 'submitbutton', 'value' => 'save'])!!} {!! Form::submit( 'Save draft', ['class' => 'btn btn-default', 'name' => 'submitbutton', 'value' => 'save-draft']) !!} 

//Controller:

 switch($request->submitbutton) { case 'save': //action save here break; case 'save-draft': //action for save-draft here break; } 
+19
source

This does not seem to work (Laravel 5.5) I tried this method to get three buttons: send, send + do and just do and hope to get names that I can configure on the switch. The first parameter of the form :: submit will be overwritten by the "value => xxx" part of the array you assigned.

So, if you are working with translations, the switch must check the language files ... https://www.dropbox.com/s/1s53gi0ufm4xz26/laravel_forms1.gif?dl=0

My solution is as indicated here Two submit buttons in one form

What HTML will look like: https://www.dropbox.com/s/grepk8leritvmj1/laravel_forms3.gif?dl=0

+1
source

An alternative solution is to assign the identifier of the submit button to the hidden input.

HTML

 <input type="hidden" id="submitOption" name="submitOption" value=""> <input type="submit" id="save" onclick="saveForm(this.id)" value="Save"> <input type="submit" id="save-draft" onclick="saveForm(this.id)" value="Save Draft"> 

Js

 function saveForm(submitOption) { $('#submitOption').val(submitOption); } 

controller

 switch($request->submitOption) { case 'save': //action save here break; case 'save-draft': //action for save-draft here break; } 
0
source

All Articles