简体   繁体   中英

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

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;
}

One of the best way is using an input with hidden type , then on clicking button append value to that input and get that value in request params in controller side . And then using if and else condition run your query.

This is hidden input:

<input id="submittype" type="hidden" name="submit_type" value=""></input>

This is the submit button div :

    <div class="text-right">
                <button class="btn btn-sm btn-primary" onclick="saveandexit()" type="submit">@lang('save &amp; exit')</button>
                <button class="btn btn-sm btn-primary" onclick="save()" type="submit">@lang('save')</button>
            <div>

This is script:
        <script>
        function save() {
              document.getElementById("submittype").value = "save";
           }
        function saveandexit() {
            document.getElementById("submittype").value = "save-and-exit";
        }
        </script>
    @endsection

And , then get in controller function:

dd($request->all());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM