Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.1 one form two submit buttons

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

like image 762
Andris Avatar asked Aug 29 '16 11:08

Andris


People also ask

Can I have two or more Submit buttons in the same form?

yes, multiple submit buttons can include in the html form. One simple example is given below.

How can we use two submit button in one form in PHP?

Having multiple submit buttons and handling them through PHP is just a matter of checking the the name of the button with the corresponding value of the button using conditional statements. In this article I'll use both elseif ladder and switch case statement in PHP to handle multiple submit buttons in a form.

Do forms need submit buttons?

If you don't have any submit button it is acceptable after all it is an element of form tag and if it is not required you may not add it with in form . This will not broke any web standard.


1 Answers

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;
}
like image 93
kscorrales Avatar answered Sep 24 '22 09:09

kscorrales