Laravel 5.1 one form two submit buttons

2019-03-27 12:26发布

问题:

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

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

I have read that Laravel won't put the submit button value in the POST when the form was sent via ajax, so could you please help me how to do this?

I have tried some 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 routes.php I have:

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

Thanks in advance

回答1:

you can use the same name and different value attribute for the 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;
}


回答2:

Seems as tho this does not work (Laravel 5.5) I tried this method to get three buttons: send, send+done and just done and hoped to get names I can then target in a switch. The first param of Form::submit will be overwritten with the "value => xxx" part of the array you assign.

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

My solution is this one, as stated here Two submit buttons in one form

That's what the HTML will look like then: https://www.dropbox.com/s/grepk8leritvmj1/laravel_forms3.gif?dl=0