多种形式提交-Laravel

时间:2019-02-28 21:39:59

标签: php laravel

我有我的 edit.blade.php 文件,如下所述

<table class="table">
                <thead>
                    <tr>
                        <th>Vendor</th>
                        <th>Item</th>
                        <th>Qty</th>
                        <th>Rate</th>
                        <th>Total</th>
                        <th>Description</th>
                        <th>Job No</th>
                    </tr>

                </thead>
                <tbody>
                    @foreach ($po_items as $po_item)
                        <tr>
                                <td><input type="text" class="form-control" name="vendor_name[]" id="vendor_name" value="{{$po_item->cost_item->vendor_company->name}}" readonly></td>
                                <td><input type="text" class="form-control" name="item_name[]" id="item_name" value="{{$po_item->cost_item->item->name}}" readonly></td>
                                <td><input type="text" class="form-control" name="qty[]" id="qty" value="{{$po_item->qty}}"></td>
                                <td><input type="text" class="form-control" name="rate[]" id="rate" value="{{$po_item->rate}}"></td>
                                <td><input type="text" class="form-control" name="total[]" id="total" value="{{$po_item->qty * $po_item->rate}}"></td>
                                <td><input type="text" class="form-control" name="description[]" id="description" value="{{$po_item->description}}"></td>
                                <td><input type="text" class="form-control" name="job_id[]" id="job_id" value="{{$po_item->cost_item->job_id}}" readonly></td>
                                <td>
                                    <form action="{{action('Employee\POItemController@destroy', $po_item->id)}}" method="post">
                                            {{csrf_field()}}
                                            {{method_field('DELETE')}}
                                            <button class="btn btn-danger" type="submit">Delete</button>
                                    </form>
                                </td>                          
                        </tr>
                    @endforeach 
                </tbody>


            </table>




            <form action="{{ route('employee.poitem.update', $vendor_company_id )}}" method="post">            
                {{csrf_field()}}
                {{method_field('PATCH')}}
                @foreach ($po_items as $po_item)
                    <input type="hidden" name="cost_item_id[]"  value="?">
                    <input type="hidden" name="qty[]" value="?">
                    <input type="hidden" name="rate[]" value="?">
                    <input type="hidden" name="description[]" value="?">
                    @endforeach          


                <button type="submit" class="btn btn-primary">Update</button>
            </form>

由于同一页面中有两个表单,因此我很难将新的表单值传递给 update函数。我阅读了使用同一会话来解决此问题的信息,但还不清楚我是否可以继续进行下去

2 个答案:

答案 0 :(得分:0)

您可以使用定位标记来代替删除表格。我不知道这是不是正确的方法,但是效果很好。

<a href="{{action('Employee\POItemController@destroy', $po_item->id)}}">Delete</a>

希望这会有所帮助:)

答案 1 :(得分:0)

您输入的ID值不正确。在HTML中,id在所有HTML标签中都应该是唯一的。当您进行foreach时:

@foreach ($po_items as $po_item)
<tr>
        <td><input type="text" class="form-control" name="vendor_name[]" id="vendor_name" value="{{$po_item->cost_item->vendor_company->name}}" readonly></td>                         
</tr>
@endforeach

您要将所有输入vendor_name分配给相同的id="vendor_name,这是W3C不正确的。您可以改用

@foreach ($po_items as $po_item)
<tr>
        <td><input type="text" class="form-control" name="vendor_name[]" id="vendor_name_{{$po_item->id}}" value="{{$po_item->cost_item->vendor_company->name}}" readonly></td>                         
</tr>
@endforeach

但这不是真正的问题,您需要提交UPDATE表单以读取每个输入数据并将其推送到隐藏的输入中。

您使用原始Javascript还是JQuery?使用JQuery,您可以更轻松地实现它,您必须在包含表单的页面上放置一些脚本。想法是找到每个输入值,例如数量,将其推入数组并将该数组转换为JSON字符串,然后将该JSON字符串作为值输入到隐藏的数量输入中。尝试以下代码:

<table class="table">
    <thead>
        <tr>
            <th>Vendor</th>
            <th>Item</th>
            <th>Qty</th>
            <th>Rate</th>
            <th>Total</th>
            <th>Description</th>
            <th>Job No</th>
        </tr>

    </thead>
    <tbody id="table_inputs">
        @foreach ($po_items as $po_item)
            <tr>
                <td><input type="text" class="form-control" name="vendor_name[]" id="vendor_name_{{$po_item->id}}" value="{{$po_item->cost_item->vendor_company->name}}" readonly></td>
                <td><input type="text" class="form-control" name="item_name[]" id="item_name_{{$po_item->id}}" value="{{$po_item->cost_item->item->name}}" readonly></td>
                <td><input type="text" class="form-control" name="qty[]" id="qty_{{$po_item->id}}" value="{{$po_item->qty}}"></td>
                <td><input type="text" class="form-control" name="rate[]" id="rate_{{$po_item->id}}" value="{{$po_item->rate}}"></td>
                <td><input type="text" class="form-control" name="total[]" id="total_{{$po_item->id}}" value="{{$po_item->qty * $po_item->rate}}"></td>
                <td><input type="text" class="form-control" name="description[]" id="description_{{$po_item->id}}" value="{{$po_item->description}}"></td>
                <td><input type="text" class="form-control" name="job_id[]" id="job_id_{{$po_item->id}}" value="{{$po_item->cost_item->job_id}}" readonly></td>
                <td>
                    <form action="{{action('Employee\POItemController@destroy', $po_item->id)}}" method="post">
                        {{csrf_field()}}
                        {{method_field('DELETE')}}
                        <button class="btn btn-danger" type="submit">Delete</button>
                    </form>
                </td>
            </tr>
        @endforeach
    </tbody>

</table>


<form id="form_update_poitem" action="{{ route('employee.poitem.update', $vendor_company_id )}}" method="post">            
    {{csrf_field()}}
    {{method_field('PATCH')}}
    @foreach ($po_items as $po_item)
        <input type="hidden" name="cost_item_id[]" >
        <input type="hidden" name="qty[]" >
        <input type="hidden" name="rate[]" >
        <input type="hidden" name="description[]" >
    @endforeach
    <button type="submit" class="btn btn-primary">Update</button>
</form>

<script>

$(document).ready(function() {

    console.log('document is ready, proceed to form update codes');

    // find button form
    var button = $('#form_update_poitem button');

    // add onclick event
    button.on('click', function(e){
        // find input array input values, turn them into array and pass that array to Hidden Input array
        var itemArr = [];
        $("#table_inputs input[type='item_name']").each(function(){
            itemArr.push($(this).val());
        });
        console.log(itemArr);
        $("#form_update_poitem input[type='cost_item_name']").val(JSON.stringify(itemArr));

        var qtyArr = [];
        $("#table_inputs input[type='qty']").each(function(){
            qtyArr.push($(this).val());
        });
        console.log(qtyArr);
        $("#form_update_poitem input[type='qty']").val(JSON.stringify(qtyArr));

        var rateArr = [];
        $("#table_inputs input[type='rate']").each(function(){
            rateArr.push($(this).val());
        });
        console.log(rateArr);
        $("#form_update_poitem input[type='rate']").val(JSON.stringify(rateArr));

        var descriptionArr = [];
        $("#table_inputs input[type='description']").each(function(){
            descriptionArr.push($(this).val());
        });
        console.log(descriptionArr);
        $("#form_update_poitem input[type='description']").val(JSON.stringify(descriptionArr));

    });

});

</script>