Laravel 5.1 one form two submit buttons

2019-03-27 12:05发布

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

2条回答
爷、活的狠高调
2楼-- · 2019-03-27 12:13

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

查看更多
混吃等死
3楼-- · 2019-03-27 12:29

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;
}
查看更多
登录 后发表回答