I have two buttons:
- one submitting the form for changing the photo and
- one deleting.
By deleting i want to send delete method to the route delete with sweet alert.
However this does not work.
How can i have one form and 2 input for submitting different form action
<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>
this is the route
Route::delete('vendor/delete/product/multiimage/{id}','vendorDeleteMultiImage')->name('vendor.delete.product.multiimage');
js code with sweetalert
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'
)
}
})
}
2
Answers
you missing the
type
attribute in the last input. just addtype='submit
and it should workYou Can do other way like Onchange function This is my Working Code
And The Javascript code look like this in success you can add whatever you want