简体   繁体   English

Laravel 5.1 一表单两个提交按钮

[英]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.我正在使用 Laravel 5.1,我想制作一个带有两个提交按钮的表单 - 保存和保存草稿。

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?我已经读过当表单通过 ajax 发送时,Laravel 不会将提交按钮值放在 POST 中,所以你能帮我怎么做吗?

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:在我的 routes.php 我有:

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.然后使用 if 和 else 条件运行您的查询。

This is hidden input:这是隐藏输入:

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

This is the submit button div :这是提交按钮 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()); dd($request->all());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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