我有一个带有 2 个按钮的表单,用于提交两个不同的表单操作和表单方法

问题描述 投票:0回答:1

我有两个按钮:

  • 提交更改照片表格并
  • 删除一个。

通过删除,我想将删除方法发送到带有甜蜜警报的路线删除。

但是这不起作用。

我如何拥有一个表单和 2 个输入来提交不同的表单操作

<tbody>
    <form  method="POST" enctype="multipart/form-data">
       @csrf
        <input type="hidden" name="old_image" value="{{$products->product_thumbnail}}">
     @foreach ($multiImages as $key => $item)
                        <tr>
                            <th scope="row">{{$key+1}}</th>
                            <td><img src="{{asset($item->photo_name)}}" alt="" style="width: 70px;height: 40px;"></td>
                            <td><input type="file" class="form-group" name="multi_img[{{$item->id}}]"></td>
                            <td>
                                <input type="submit" formaction="{{route('vendor.update.product.multiImage',$item->id)}}" class="btn btn-primary px-4"value="Save Product"  >
                            </td>
                            <td>
                                <input  id="delete" onclick="submitResult(event)" data-method="delete" data-action="{{route('vendor.delete.product.multiimage',$item->id)}}" value="Delete Product" class="form-group btn-danger" >    
                            </td>
                        </tr>
                        @endforeach
                        
    
                    </form>
                </tbody>

这是路线

Route::delete('vendor/delete/product/multiimage/{id}','vendorDeleteMultiImage')->name('vendor.delete.product.multiimage');

带有 sweetalert 的 js 代码

function submitResult(e) {
    let btn = e;
    e.preventDefault();
    
    Swal.fire({
        title: 'Are you sure?',
        text: "You won't be able to revert this!",
        icon: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#3085d6',
        cancelButtonColor: '#d33',
        confirmButtonText: 'Yes, delete it!'
    }).then((result,e) => {
        if (result.isConfirmed) {
            btn.target.form.action = btn.target.getAttribute('data-action');
            btn.target.form.method = btn.target.getAttribute('data-method');
            var input = document.createElement("input");

           
            input.setAttribute("type", "hidden");
            input.setAttribute("name", "_method");
            input.setAttribute("value", "DELETE");
            //append to form element that you want .
            btn.target.form.appendChild(input);
            btn.target.form.submit();
            Swal.fire(
                'Deleted!',
                'Your file has been deleted.',
                'success'
            )
        }
        
    })

}
javascript laravel form-submit sweetalert delete-method
1个回答
0
投票

您在最后一个输入中缺少

type
属性。只需添加
type='submit
就可以了

© www.soinside.com 2019 - 2024. All rights reserved.