I am trying to delete a record with double confirmation of sweet alert from a datatables, but when selecting one, it always sends me the id 1, regardless of whether it is the record with id 10, 20 or 30. Why is this or what am I doing? doing wrong? Annex code that generates the list
@foreach ($array as $row)
<tr>
<td>
<img src="{{ asset('uploads/users/'.$row->login.'/image.png') }}" style="max-width:30px;">
{{ $row->firstName.' '.$row->lastName }}
</td>
<td>
{{ $row->email }}
</td>
<td>
{{ $row->login }}
</td>
<td>
<!-- Action buttons -->
<div>
{!! Form::open(array('route' => array($options['route'].'.destroy', $row->id), 'method' => 'DELETE', 'id' => 'myform'.$row->id)) !!}
<a href="{{ route($options['route'].'.show', $row->id) }}" class="btn btn-warning btn-sm">
<i class="la la-small la-edit"></i>
</a>
<button type="button" id="delete" data-id="<?php echo $row->id; ?>" class="btn btn-danger btn-sm"><i class="la la-small la-trash"></i></button>
{!! Form::close() !!}
</div>
</td>
</tr>
@endforeach
and the js function that generates the Sweet alert
$('button#delete').on('click', function() {
var id = $(this).attr('data-id');
swal({
title: "¿Desea eliminar el usuario?",
text: "",
type: "warning",
showCancelButton: true,
confirmButtonClass: "btn-danger",
confirmButtonText: "Si!!",
cancelButtonText: "No!!",
closeOnConfirm: false,
closeOnCancel: false
},
function(isConfirm) {
if (isConfirm) {
swal({
title:'¡El usuario será eliminado!',
text: '',
type: 'success'
},
function() {
$("#myform"+id).submit();
});
} else {
swal("Cancelled", "El usuario no será eliminado!!", "error");
}
});
})
Your problem is that you are repeating the same id and that should not be done, remember that the id must be unique, work with classes for that, .delete would be more useful
change this
For this
remember that you can also rely on the data attributes to add, for example, the id to identify the button where you clicked, I say this because of the fact of trying to identify the form that you are going to send.
Change this too leave it like this:
to your button put this data attribute:
and you switalert like this:
that way it identifies which form to send when clicking a button